At work, I also learned that Pack,unpack,ord is powerful for binary byte processing. Let me introduce them to you. In our work, there is not much estimation of their use. I am in a recent job, because the communication needs to use the binary stream, and then the interface is received in PHP. At that time, a lot of information was consulted. Because they are really less used, they are seldom used by friends in their work. At work, I also learned that Pack,unpack,ord is powerful for binary byte processing. Let me introduce them to you.
pacrk Function Description : This function is used to compress data into a string in place.
Syntax : Pack (format,args+)
Parameters |
Describe |
Format |
Necessary. Specifies the format to use when wrapping the data. |
args+ |
Optional. Specifies one or more parameters that are packaged. |
Character |
Description |
A |
Fills the string blank with a NULL character |
A |
Fills the string blank with space characters (spaces) |
H |
16 string, low in front |
H |
16 string, high in front |
C |
Character with number |
C |
No number character |
S |
Number of short integers (16 bits, depending on the computer's bit order) |
S |
No number short integer (16 bits, depending on the computer's bit order) |
N |
unsigned short integers (16-bit, high-order in the back) |
V |
No number short integers (16 bits, low in the back order) |
I |
Number of integers (depending on the order and scope of the computer) |
I |
unsigned integers (depending on the computer's order and range) |
L |
Number of long integers (32 bits, depending on the computer's bit order) |
L |
unsigned long integers (32 bits, depending on the computer's bit order) |
N |
unsigned short integers (32-bit, high-order in the back) |
V |
No number short integers (32 bits, low in the back order) |
F |
Single-precision floating-point number (depending on the computer's range) |
D |
Times the exact floating point number (depending on the computer's range) |
X |
Vacancy |
X |
Rewind One |
@ |
Fill in the NULL character to the absolute position |
Unpack Function Description: This function is used to extract the data of the bit string
Syntax : Unpack (format,args+)
Parameters |
Describe |
Format |
Necessary. Specifies the format to use when wrapping the data. |
args+ |
Optional. Specifies one or more parameters that are packaged. |
The parameters are the same as the pack.
Ord function Description: Returns the Acill code value of the corresponding character
Syntax : Ord ($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-shaped 32-bit 4-byte 64-bit 8-byte
$str = (Pack ("I", "100"));
echo $str, "=", strlen ($STR), "Byte \ n";
Getascill ($STR);
s character short shaping 2 bytes
$str = (Pack ("s", "100"));
echo $str, "=", strlen ($STR), "Byte \ n";
Getascill ($STR);
L character-length shaping 4 bytes
$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 "=============\r\n\r\n";
}
?>
With the above example, we can see that the same string, stored in a different format, consumes a different number of bytes. It can also be seen here that saving characters in different formats can save space for storage. and start the unreadable encryption effect. Suddenly think of a bit, Design database field type problems, if a field is just: 10-bit length integer type. We set it to reshape: 256*256*256*256 is 4 bytes, if set to 10 length string. That accounts for 10 bytes. The whole digestive space is twice times the size. Setting the correct character type is a great help in improving database performance. Oh, a little off-topic ...
A case analysis of PHP processing byte code communication
Just said pack function: space-saving, encrypted format
Here are 2 examples to illustrate, interface development requirements:
Parameters |
Describe |
User name |
20 bytes, character type |
Password |
10 bytes, Character type |
Age |
1 bytes, no character char type |
Date of birth |
4 bytes, integer (19800101) |
Mailbox |
50 bytes, string |
Between fields: "\" split |
A, pack Packaging
[PHP]View Plaincopyprint?
- $code =Array (
- "username" = =Array ("A20","Zhang San adfb12"),
- "Pass" =Array ("A10","asdf* #1"),
- "Age" = =Array ("C", "All"),
- "Birthday" = =Array ("I","19900101"),
- "Email" = =Array ("A50","[email protected]"));
- $stream =join ("n", Packbyarr ($code));
- Echo $stream,strlen ($stream);
- File_put_contents ("C:/1.txt",$stream); //Save the stream for easy reading below.
- 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";
- }
- }
The entire length is 89 bytes, because it is split with "\". With the above output, some string output can be read, the others have become garbled. This is what I said can be kept secret effect reason.
B, unpack unpacking
The unpacking needs to be read in a packaged manner, the length of the read, what type to read, and the same as the packing rules.
[PHP]View Plaincopyprint?
- $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 ("n",$str);
- $ATARR =Array ();
- $i = 0;
- foreach ($code as $k = =$v) {
- $ATARR [$k]=unpack ($v [0],$Arr [$i]);
- $i + +;
- }
- return $ATARR;
- }
PHP pack, unpack binary stream interface application instance