PHP park unpark ord function usage method (binary stream interface application example)

  • 2020-03-31 21:21:38
  • OfStack

The three functions of park,unpark and ord are not used much in our work. In my last job, because the communication required a binary stream, the interface was received in PHP. When processing at that time, consult a lot of data. Because they are actually used less, and few of my friends use them at work. In my work, I gradually learned about the power of park,unpark and ord in binary byte processing. Let me introduce them one by one.

Introduction to the use of park,unpark and ord functions

Park function description : this function is used to compress and package data into a string.

 

grammar : pack (format, args +)

parameter describe The format A necessity. Specifies the format to use when wrapping the data. The args + Optional. Specifies one or more parameters to be wrapped.

 

character instructions a. Fills the string blank with NULL characters a. Fills the string blank with the SPACE character H. Hexadecimal string, preceded by the low bit H. Hexadecimal string with the highest digit first c Have no character C No. No character s. Short numbered integer (sixteen bits, in bit order of the computer) s. Unsigned short integer (sixteen bits, in bit order of the computer) n Unsigned short integers (16 bits, high order) v Unsigned short integers (16 bits, low order) i. Signed integers (in the order and range of the computer) i. Unsigned integer (in the order and range of the computer) l Numbered long integer (32 digits, in bit order of the computer) L Unsigned long integer (32 digits, in bit order of the computer) N Unsigned short integers (32 digits, order after the high) V Unsigned short integers (32 digits, low order) f Single exact floating point number (depending on the range of the computer) d Multiple exact floating point (depending on the computer's range) x vacancy X Fall back to a @ Fill the NULL character into the absolute position

 

Description of unpark function: This function is used to extract the data of a string of bits

grammar : unpack (format, args +)

parameter describe The format A necessity. Specifies the format to use when wrapping the data. The args + Optional. Specifies one or more parameters to be wrapped.

The parameters are the same as park.

Ord function description: Returns the acill code value of the corresponding character

grammar : the word ($character);

Example description:

 
<?php 
//A character
$str=(pack("A*", " China ")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 
//H character
$str=(pack("H*", "fffe")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 
//C character
$str=(pack("C*", "55","56","57")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 

//I character short orthopedic 32 bits 4 bytes 64 bits 8 bytes
$str=(pack("i", "100")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 

//The s character is shortened by 2 bytes
$str=(pack("s", "100")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 

//The l character is 4 bytes long
$str=(pack("l", "100")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 

//F character single precision floating point 4 bytes
$str=(pack("f", "100")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 

//D character double precision floating point 8 bytes
$str=(pack("d", "100")); 
echo $str,"=",strlen($str)," byte n"; 
getAscill($str); 

function getAscill($str) 
{ 
$arr=str_split($str); 
foreach ($arr as $v) 
{ 
echo $v,"=",ord($v),"n"; 
} 
echo "=============rnrn"; 
} 


From the above example, we can see that the same string, stored in different formats, takes up different bytes. You can also see here that saving characters in different formats can save space. And unreadable encryption effect. A sudden thought, design database field type problem, if a field is only: 10 - bit length integer. We set the integer: 256*256*256*256 for 4 bytes, if set to 10 length strings. That's 10 bytes. The total digestion space is 2 times. Setting the right character type can help a lot to improve database performance. Hehe, a little bit off topic...

Example analysis of bytecode communication in PHP
What pack does: save space, encrypt format

The following two to do an example, interface development requirements:
parameter describe The user name 20 bytes, character type password 10 bytes, character type age 1 byte, unsigned char type Date of birth 4 bytes, integer (19800101) email 50 bytes, string Use "\0" to divide A and PACK between each field
 
<?php 
$code=array( 
"username"=>array("A20"," Zhang SAN adfb12"), 
"pass"=>array("A10","asdf*#1"), 
"age"=>array("C","23"), 
"birthday"=>array("I","19900101"), 
"email"=>array("A50","zhangsan@163.com")); 

$stream=join("0",parkByArr($code)); 
echo $stream,strlen($stream); 


 
file_put_contents("c:/1.txt",$stream);//Save the stream for easy reading below

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"; 
} 
} 

Because of the "\0" partition, the entire length is 89 bytes. With the output above, some of the string output is readable, and the rest is garbled. That's why I said it would work.
A. Unpack b. Unpack c. Unpack d. Unpack
Unpacking needs to be read in the way that it is packaged, how long it is read, and what type of reading it should be used, which must be the same as the packaging specification.
 
<?php 
$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(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; 
} 

< img border = 0 SRC = "http://files.jb51.net/upload/201010/20101019024501773.gif" border = 0 >


Related articles: