Analytic binary Flow Interface Application Instance pack, unpack, Ord function _php technique

Source: Internet
Author: User
Tags ord unpack
At work, I've come to understand that Pack,unpack,ord is powerful for binary byte processing. Let me introduce them each. In our work, there are not many estimates to use them. In my most recent job, because communication requires a binary stream, the interface is received in PHP. At that time, a lot of information was consulted. Because they are less used, and few friends use them in their work. At work, I've come to understand that Pack,unpack,ord is powerful for binary byte processing. Let me introduce them each.
Pack Function Description:This function is used to package data compression into a string in place.
Syntax: Pack (format,args+)
parameter Description
Format required. Specify the format to use when wrapping the data.
Args+ Optional. Specify one or more parameters to be wrapped.
Character description
A fills the string blank with a NULL character
A fills the string blank with space characters (spaces)
H 16 carry string, low in front
H 16 carry string, high in front
C has a number character
C no-number characters
S has a short integer (16 bits, depending on the bit order of the computer)
S-unsigned short integer (16 bits, by computer's bit order)
n Short-unsigned integers (16-bit, high in the latter order)
V unsigned short integers (16-bit, low in the back order)
I have a number of integers (according to the Order and range of the computer)
I-No integer (in the order and range of the computer)
l have long integers (32 bits, by computer bit order)
L unsigned long integers (32 bits, by computer bit order)
N short-unsigned integers (32-bit, high in the latter order)
V unsigned short integers (32-bit, low in the back order)
F single precise floating-point number (depending on the scope of the computer)
D-Times exact floating-point number (depending on the scope of the computer)
X vacancy
X Rewind A
@ Fill in NULL characters to absolute position
Unpack function Description:This function is used to decompress the data of a bit string
Syntax: Unpack (format,args+)
parameter Description
Format required. Specify the format to use when wrapping the data.
Args+ Optional. Specify one or more parameters to be wrapped.
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:
Copy Code code as follows:

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

From the example above, we can see that the same string, stored in different formats, occupies a different number of bytes. You can also see that saving characters in different formats can save space for storage. and open to unreadable encryption effect. Suddenly think of a point, Design database field type problem, if a field is just: 10-bit length integral type. We are set to reshape: 256*256*256*256 is 4 bytes, if set to 10 length strings. That's 10 bytes. The whole digestive space is twice times. Setting the correct character types can help improve database performance. Oh, a bit off the topic ...
A case analysis of byte code communication in PHP processing
Just say the pack role: space-saving, encryption format
Here are 2 examples to illustrate the interface development requirements:
parameter Description
User name 20 bytes, character type
Password 10 bytes, character type
Age 1 Byte, no character char
Date of birth 4 bytes, integral type (19800101)
Mailbox 50 Bytes, string
Between the fields using: "" "split
A, pack envelope
Copy Code code as follows:

<?php
$code =array (
"Username" =>array ("A20", "John Adfb12"),
"Pass" =>array ("A10", "asdf* #1"),
"Age" =>array ("C", "23"),
"Birthday" =>array ("I", "19900101"),
"Email" =>array ("A50", "zhangsan@163.com"));
$stream ("=join", 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 "". Through the above output, there are some string output can be read, the others have become garbled. That's why I said I could keep it secret.
B, Unpack solution Package
Unpack needs to be read by, packaged, how long the read, what type of read, must be the same as packing rules.
Copy Code code as follows:

<?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 (Packbyarr ($stream, $code));
function Packbyarr ($STR, $code) {
$ARR =explode ("$str");
$ATARR =array ();
$i = 0;
foreach ($code as $k => $v) {
$ATARR [$k]=unpack ($v [0], $ARR [$i]);
$i + +;
}
return $ATARR;
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.