php使用pack处理二进制文件的方法(2)

<?php $code=array( "username"=>array("A7","张三adfb12"), "pass"=>array("A10","asdf*#1"), "age"=>array("C","23"), "birthday"=>array("I","19900101"), "email"=>array("A50","www.jb51.net")); $stream=join("\0",parkByArr($code)); echo $stream,strlen($stream); file_put_contents("1.txt",$stream);//将流保存起来便于下面读取 function parkByArr($arr) { $atArr=array(); foreach ($arr as $k=>$v) { $atArr[]=pack($v[0],$v[1]); } return $atArr; } function getAscill($str) { $arr=str_split($str); foreach ($arr as $v) { echo $v,"=",ord($v),"\n"; } } $code=array( "username"=>array("A20"), "pass"=>array("A10"), "age"=>array("C"), "birthday"=>array("I"), "email"=>array("A50")); $stream=file_get_contents("1.txt"); var_dump(parkByArr($stream,$code)); function parkByArr($str,$code) { $Arr=explode("\0",$str); $atArr=array(); $i=0; foreach ($code as $k=>$v) { $atArr[$k]=unpack($v[0],$Arr[$i]); $i++; } return $atArr; }

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/16920acd2b6fbf79d9825ab79151f2e8.html