/*********************************************************************************
* 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的更多相关文章

  1. 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 ...

  2. byte数组和File,InputStream互转

    1.将File.FileInputStream 转换为byte数组: File file = new File("file.txt"); InputStream input = n ...

  3. Byte Array to Hexadecimal String

    Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...

  4. C# byte array 跟 string 互转

    用 System.Text.Encoding.Default.GetString() 转换时,byte array 中大于 127 的数据转 string 时会出问题. 把这里的 Default 换成 ...

  5. C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:

    public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ...

  6. DBus send byte array over gdbus ----Send dbus data

    遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传  a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ...

  7. XAF 如何将数据库中Byte array图片显示出来

    问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...

  8. [原] XAF 如何将数据库中Byte array图片显示出来

    问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...

  9. Convert PIL Image to byte array?

    1.import io img = Image.open(fh, mode='r') roiImg = img.crop(box) imgByteArr = io.BytesIO() roiImg.s ...

随机推荐

  1. SpringData关键字查询练习

    我们在上一节知道SpringData关键字有很多,我就拿几个例子练练手 1.需求我们查询lastName like sun and id < ?的查询 package com.fxr.sprin ...

  2. Python: 复数的数学运算

    写的最新的网络认证方案代码遇到了一个难题,唯一的解决办法就是使用复数空间,需要使用复数来执行一些计算操作. 复数可以用使用函数complex(real, imag) 或者是带有后缀j 的浮点数来指定. ...

  3. RedHot 不能联网 ifconfig命令只显示lo 不显示eth0的解决方法!

    1.修改eth0 具体步骤如下 vi /etc/sysconfig/network-scripts/ifcfg-eth0, i,进入insert编辑模式,具体参数设置如下: DEVICE=eth0 B ...

  4. 使用spring cache和ehcache之前必须了解的

    好长时间没写博客了,真的是没时间啊.ps:其实就是懒!!接下来几篇要写下缓存,这里主要写下ehcache与spring整合的内容,包括aop形式的缓存,基于注解的缓存,页面缓存这三方面吧.在这之前先要 ...

  5. Spring的一些面试题(转)

    一.spring工作原理: 1.spring mvc的所有请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求进行真正的处理工作.2.DispatcherServlet ...

  6. router基本使用

    摘自:https://blog.csdn.net/qq_39894133/article/details/78992923 1.vue2 路由的使用流程: 1.vue2中的路由定义层:<rout ...

  7. LabView和DLL中的参数问题

    注意:在LabView中调用DLL函数时,一定要指定对应的参数类型,而不是选择“Adapt to Type”,否则会出错,不知道为什么书上是要选择“Adapt to Type”. 以下做个参考: 转自 ...

  8. 从IC设计业看中国企业之发展

    从IC设计业看中国企业之发展   在半导体领域,国际平均毛利润水平为40%.去年IC设计年会中,中国半导体行业协会IC设计分会理事长魏少军指出,中国IC设计业平均毛利润水平比国际平均水平低了12.39 ...

  9. 设置VS快捷代码片段

    一.自定义sinppets方式 1.在VS安装路径[D:\vs2013\VC\Snippets\2052\Visual C++]下新建一个snippt文件 2.添加代码 <?xml versio ...

  10. ubuntu16.04下安装TensorFlow(GPU加速)----详细图文教程【转】

    本文转载自:https://blog.csdn.net/zhaoyu106/article/details/52793183 le/details/52793183 写在前面 一些废话 接触深度学习已 ...