/*********************************************************************************
* 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. marathon-lb-什么是服务发现?(转)

    摘要: 将容器应用部署到集群时,其服务地址,即IP和端口, 是由集群系统动态分配的.那么,当我们需要访问这个服务时,如何确定它的地址呢?这时,就需要服务发现(Service Discovery)了.本 ...

  2. (1)R介绍

    1. R初窥 从CRAN(The Comprehensive R Archive Network)cran.r-project.org—mirrors.html中选择一个镜像,然后下载合适的安装包(R ...

  3. sql server递归子节点、父节点,sql查询表结构,根据字段名查所在表

    一.查询当前部门下的所有子部门 WITH dept AS ( SELECT * FROM dbo.deptTab --部门表 WHERE pid = @id UNION ALL SELECT d.* ...

  4. 一次org.springframework.jdbc.BadSqlGrammarException ### Error querying database Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException问题排查过程

    先说结论: 因为在表设计中有一个商品描述字段被设置为desc,但desc是mysql中的关键字,如select id,name,desc,price from product;这条sql语句在查询时的 ...

  5. spring AOP的两种代理

    本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理  2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...

  6. Springmvc的拦截器执行顺序及各方法作用

    实现HandlerInterceptor接口或者继承HandlerInterceptor的子类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInt ...

  7. 20145309李昊《网络对抗》MSF应用基础

    实验内容 掌握metasploit的基本应用方式1.主动攻击——ms08_0672.针对浏览器的攻击——ms11_0503.针对客户端的攻击——Adobe4.成功应用一个辅助模块——scanner/d ...

  8. kotlin 学习感受

    目录 kotlin 学习感受 特点 优点 屎一样的缺点 总结 kotlin 学习感受 直白的说..很不好,像屎一样,本来对此抱有很大的期望和好感度,但经过一整天的学习,完全失望了,这门语言中间有各种的 ...

  9. 编译 Tensorflow 1.10 + CUDA9.2 + MKL【转】

    本文转自:https://www.solarck.com/compile-tensorflow-gpu.html 我的电脑系统是基于 Archlinux 的 Manjaro,软件包更新的比较激进,很早 ...

  10. Lucene 基础知识

    1. 数据分类 结构化数据: 指具有固定格式或有限长度的数据,如数据库等; 非结构化数据: 指不定长或无固定格式的数据, 如邮件,word 文档等磁盘上的文件; 1.1 非结构化数据查询方法 顺序扫描 ...