zoukankan      html  css  js  c++  java
  • PHP write byte array to file

    /*********************************************************************************
     *                    PHP write byte array to file
     * 说明:
     *     遇到需要将byte array写入file,结果找不到专门的字节写入的方法。
     *
     *                                              2017-10-23 深圳 南山平山村 曾剑锋
     ********************************************************************************/
    
    一、参考文档:
        1. 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
        2. pack
            http://php.net/manual/en/function.pack.php
        3. 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 "1", but the integer 1 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、shortintlongfloat、double都要通过指定pack个参数类设定。
    
    四、Example:
        <?php
            $eeprom_size = 256;
        
            $bytes = array();
            $bytes = array_pad($bytes, $eeprom_size, 0);
        
            $mac_address = "11:22:33:44:55:66";
            $macArray = explode(':', $mac_address);
        
            print_r($macArray);
            echo "".hexdec($macArray[0])."
    ";
            echo "".hexdec($macArray[1])."
    ";
            
            # echo '{"status": "ok", "MAC": "'.$mac_address.'"}';
            $bytes[0] = 0x01;
            $bytes[1] = 0x06;
            $bytes[2] = hexdec($macArray[0]);
            $bytes[3] = hexdec($macArray[1]);
            $bytes[4] = hexdec($macArray[2]);
            $bytes[5] = hexdec($macArray[3]);
            $bytes[6] = hexdec($macArray[4]);
            $bytes[7] = hexdec($macArray[5]);
            $bytes[8] = 0x00;
        
            $bytes[0xfe] = 0x03;
            $bytes[0xff] = 0x00;
        
            print_r($bytes);
        
            echo '{"status": "ok", "MAC": "'.hexdec($macArray[5]).""}
    ";
        
            $ptr = fopen("./eeprom", 'wb');
            for ($i = 0; $i < $eeprom_size; $i++) {
                fwrite($ptr, pack('C', $bytes[$i]));
            }
            fclose($ptr);
        ?>
        
  • 相关阅读:
    django框架之有名分组、无名分组、路由分发、反向解析等相关内容-62
    django框架之请求生命周期及ORM引入等相关内容-61
    django框架之目录介绍等相关内容-60
    django框架之引入及基础介绍等相关内容-59
    前端之JavaScript练习等相关内容-58
    django之jwt多方式登陆及多表群操作增删改查等相关内容-90
    Linux基础之虚拟机创建、网卡激活、切换YUM源及用户级别等相关内容-92
    django框架之auth组件等相关内容-76
    django框架之单表查询等相关内容-67
    自律
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/7717110.html
Copyright © 2011-2022 走看看