/*********************************************************************************
* 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. java基础知识面试题(41-95)

    41.日期和时间:- 如何取得年月日.小时分钟秒?- 如何取得从1970年1月1日0时0分0秒到现在的毫秒数?- 如何取得某月的最后一天?- 如何格式化日期?答:问题1:创建java.util.Cal ...

  2. The adidas NMD Camo Singapore consists of four colorways

    Next within the popular selection of the adidas NMD Singapore is really a clean all-black form of th ...

  3. (20)Cocos2d-x中的引用计数(Reference Count)和自动释放池(AutoReleasePool)

    引用计数 引用计数是c/c++项目中一种古老的内存管理方式.当我8年前在研究一款名叫TCPMP的开源项目的时候,引用计数就已经有了. iOS SDK把这项计数封装到了NSAutoreleasePool ...

  4. active admin常用配置

    ActiveAdmin.register Post do permit_params :title, :content, :deadline, :status menu parent: "论 ...

  5. Ubuntu安装 Spark2.3.0 报错原因及解决

    Ubuntu 安装Spark出现的问题及解决 最近在搭建Hadoop集群环境和Spark集群环境,出现的问题可能不太复杂,纯粹记录安装步骤和问题解决办法.集群环境使用的是(2台)阿里云主机,操作系统是 ...

  6. g711u与g729比较编码格式

    •g711a—编解码格式为G.711 alaw •g711u—编解码格式为G.711 ulaw (the default) •g729—编解码格式为G.729 •g729a—编解码格式为G.729a ...

  7. Spring Cloud OAuth2(一) 搭建授权服务

    概要 本文内容主要为spring cloud 授权服务的搭建,采用jwt认证. GitHub 地址:https://github.com/fp2952/spring-cloud-base/tree/m ...

  8. Python学习札记(三十) 面向对象编程 Object Oriented Program 1

    参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...

  9. nodejs真的是单线程吗?

    [原文] 一.多线程与单线程 像java.python这个可以具有多线程的语言.多线程同步模式是这样的,将cpu分成几个线程,每个线程同步运行. 而node.js采用单线程异步非阻塞模式,也就是说每一 ...

  10. python 获取5天前的日期

    from datetime import date, timedelta dt = date.today() - timedelta() print('Current Date :',date.tod ...