PHP write byte array to file
/*********************************************************************************
* PHP write byte array to file
* 说明:
* 遇到需要将byte array写入file,结果找不到专门的字节写入的方法。
*
* 2017-10-23 深圳 南山平山村 曾剑锋
********************************************************************************/ 一、参考文档:
. In PHP, how to write one unsigned byte value to a file at a given offset?
https://stackoverflow.com/questions/38556346/in-php-how-to-write-one-unsigned-byte-value-to-a-file-at-a-given-offset
. pack
http://php.net/manual/en/function.pack.php
. file
http://php.net/manual/zh/function.file.php 二、解决办法:
The output of pack is not string characters. Generally, write functions in PHP only deal with strings, no matter what you give it. Here you have to note that although the output of pack is string, but it does not contains charatcer "", but the integer itself. If you echo $b you will see "\x01" which means the byte you are looking for. 三、pack使用说明:
PHP写入文件内容使用了统一的String类型,通过pack可以将数据变成统一的String类型,而通过给pack不同的参数,可以将不同的数据封装在String类型里。所以要写入byte、short、int、long、float、double都要通过指定pack个参数类设定。 四、Example:
<?php
$eeprom_size = ; $bytes = array();
$bytes = array_pad($bytes, $eeprom_size, ); $mac_address = "11:22:33:44:55:66";
$macArray = explode(':', $mac_address); print_r($macArray);
echo "".hexdec($macArray[])."\r\n";
echo "".hexdec($macArray[])."\r\n"; # echo '{"status": "ok", "MAC": "'.$mac_address.'"}';
$bytes[] = 0x01;
$bytes[] = 0x06;
$bytes[] = hexdec($macArray[]);
$bytes[] = hexdec($macArray[]);
$bytes[] = hexdec($macArray[]);
$bytes[] = hexdec($macArray[]);
$bytes[] = hexdec($macArray[]);
$bytes[] = hexdec($macArray[]);
$bytes[] = 0x00; $bytes[0xfe] = 0x03;
$bytes[0xff] = 0x00; print_r($bytes); echo '{"status": "ok", "MAC": "'.hexdec($macArray[])."\"}\r\n"; $ptr = fopen("./eeprom", 'wb');
for ($i = ; $i < $eeprom_size; $i++) {
fwrite($ptr, pack('C', $bytes[$i]));
}
fclose($ptr);
?>
PHP write byte array to file的更多相关文章
- Convert a byte[] array to readable string format. This makes the "hex" readable!
/* * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol ...
- byte数组和File,InputStream互转
1.将File.FileInputStream 转换为byte数组: File file = new File("file.txt"); InputStream input = n ...
- Byte Array to Hexadecimal String
Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...
- C# byte array 跟 string 互转
用 System.Text.Encoding.Default.GetString() 转换时,byte array 中大于 127 的数据转 string 时会出问题. 把这里的 Default 换成 ...
- C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:
public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ...
- DBus send byte array over gdbus ----Send dbus data
遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传 a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ...
- XAF 如何将数据库中Byte array图片显示出来
问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...
- [原] XAF 如何将数据库中Byte array图片显示出来
问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...
- Convert PIL Image to byte array?
1.import io img = Image.open(fh, mode='r') roiImg = img.crop(box) imgByteArr = io.BytesIO() roiImg.s ...
随机推荐
- Selenium之IE浏览器的启动问题及解决
前面有篇文章说到启动IE浏览器时,会出现以下错误提示: 浏览器启动之后,页面不会自动输入代码设置的地址,如下图展示 查看报错语句,发现原来是浏览器比例调的不正确,修改浏览器比例为100%即可解决该问题
- js判断用户是在PC端或移动端访问
js如何判断用户是在PC端和还是移动端访问. 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的体验.伴随Android平台的红火发展.不仅带动国内智能手机行业,而 ...
- Kylo 入坑记
一.概述 Kylo,作为一个基于 Spark 和 NiFi 的开源数据湖编排框架,解决对数据湖获取.治理.感知和技术支持等诸多问题.Kylo 将数据湖的很多功能自动化,包括数据接入.准备.分析发现.P ...
- AtCoder Beginner Contest 115 Solution
A Christmas Eve Eve Eve Solved. #include <bits/stdc++.h> using namespace std; int main() { int ...
- Python 自带IDLE 如何打开
- laravel 多图上传
前台 name="photo[]" 后台获取 $request->file('photo');//获取多个图片循环
- Struts2中struts.multipart.maxSize配置
今天使用Struts2的文件上传控件时,在struts.xml中,将处理上传的action中的fileUpload拦截器的maximumSize参数设置为5000000,上传了一个3M的文件后发现控制 ...
- 20145311 《Java程序设计》第七周学习总结
20145311 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 Lambda Lambda表达式会使程序更加地简洁,在平行设计的时候,能够进行并行处理. 第十三章 时间与 ...
- FromBottomToTop第十三周项目博客
FromBottomToTop第十三周项目博客 本周项目计划 完成游戏核心算法以及界面相关类和怪物类 项目进展 用户可选择游戏模式,共有20张不同的地图. 炮台的建立和升级. 小怪的路径算法. 参考资 ...
- python 随机整数
# Program to generate a random number between and # import the random module import random print(ran ...