时间:2021-07-01 10:21:17 帮助过:3人阅读
//A字符 $str = (pack ( "A*", "中国" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //H字符 $str = (pack ( "H*", "fffe" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //C字符 $str = (pack ( "C*", "55", "56", "57" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //i字符 短整形 32位 4个字节 64位8个字节 $str = (pack ( "i", "100" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //s字符 短整形 2个字节 $str = (pack ( "s", "100" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //l字符 长整形 4个字节 $str = (pack ( "l", "100" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //f字符 单精度浮点 4个字节 $str = (pack ( "f", "100" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; //d字符 双精度浮点 8个字节 $str = (pack ( "d", "100" )); echo $str, "=", strlen ( $str ), "字节\n"; getAscill ( $str ); echo '
'; function getAscill($str) { $arr = str_split ( $str ); foreach ( $arr as $v ) { echo $v, "=", ord ( $v ), "\n"; } echo "=============\r\n\r\n"; }
$code=array( "username"=>array("A20","张三adfb12"), "pass"=>array("A10","asdf*#1"), "age"=>array("C","23"), "birthday"=>array("I","19900101"), "email"=>array("A50","zhangsan@163.com")); $stream=join("\0",packByArr($code)); echo $stream,strlen($stream); file_put_contents("c:/1.txt",$stream); //将流保存起来便于下面读取 function packByArr($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("c:/1.txt"); var_dump(packByArr($stream,$code)); function packByArr($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; }