/*********************************************************************************
* 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. VMware跨电脑移动Linux虚拟机

    环境:VMware-Workstation-12-Pro,Windows-10,CentOS-6.9-x86_64,Xshell5 概况 vmware自带的快照,克隆功能,都可以实现备份虚拟机的功能, ...

  2. wordpress的安装及使用

    1.如何查看别人的wordpress站点所有的模板 2.如何使用自定义的模板

  3. 统计编程的框架与R语言统计分析基础——摘(2)统计分析之线性回归

    一.线性回归 1.简单线性回归 a. > x = women > x height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 ...

  4. jvm之gc日志

    ava GC日志可以通过 +PrintGCDetails开启 以ParallelGC为例 YoungGC日志解释如下 FullGC:

  5. vim tab设置为4个空格

    为了vim更好的支持python写代码,修改tab默认4个空格有两种设置方法: 1. vim /etc/vimrc 1 set ts=4 2 set sw=4 2. vim /etc/vimrc 1 ...

  6. Python通用网络爬虫脚本

    from sys import argv from os import makedirs,unlink,sep,mkdir from os.path import dirname,exists,isd ...

  7. hdu3037 Saving Beans(Lucas定理)

    hdu3037 Saving Beans 题意:n个不同的盒子,每个盒子里放一些球(可不放),总球数<=m,求方案数. $1<=n,m<=1e9,1<p<1e5,p∈pr ...

  8. Python3.x:简单时间调度Timer(间隔时间执行)

    Python3.x:简单时间调度Timer(间隔时间执行) threading模块中的Timer能够帮助实现定时任务,而且是非阻塞的: 代码: import threading import time ...

  9. TrueCrypt简介及TrueCrypt 7.1a Source.zip源码在VS2008下的编译过程

    转载:http://blog.csdn.net/cncrypt/article/details/51565493 转载:http://www.cnblogs.com/shenjieblog/p/521 ...

  10. [经验分享]SecureCRT导出操作日志 + Notepad自定义语言格式高亮日志文件

    起因及效果展示 最近使用CRT,有些命令会输出很多内容,这时如果你想要得知输出内容是从哪里开始的,很容易被大量的同种颜色的文字搞的晕头转向.如果输入的命令是不同的颜色,这会大大得帮助我们. 所谓的命令 ...