How to use the pack, unpack, and ord functions of the application instance for parsing the binary stream interface

Source: Internet
Author: User
Tags unpack
This article provides a detailed analysis of how to use the pack, unpack, and ord functions of binary stream interface application instances. For more information, see pack, unpack and ord are powerful in processing binary bytes. Next I will introduce them one by one. In our work, we do not have much estimation to use them. In my recent work, binary stream is used for communication, and the interface is received in php. At that time, I checked a lot of materials. Because they are rarely used, and few friends will use them at work. At work, I gradually learned that pack, unpack, and ord are powerful in processing binary bytes. Next I will introduce them one by one.
Pack function description:This function is used to compress and package data into strings.
Syntax:Pack (format, args +)
Parameter description
Format is required. The format used for packaging data.
Args + optional. Specify one or more parameters to be packaged.
Character Description
A fills up the blank string with NULL characters
A fills up the blank string with spaces
H hexadecimal string, before low
H: a hexadecimal string with a high position in front
C character
No. C characters
S has a short integer (sixteen digits, in the computer's bit order)
S no-number short integer (sixteen digits, in the computer's bit order)
N No-number short integer (sixteen bits, the order in which the high positions are placed)
V no-number short integer (sixteen bits, the order after the low position)
I have an integer (in computer order and range)
I unnumbered integer (in computer order and range)
L long integer with numbers (two digits of numbers, in the computer's bit order)
L no-number long integer (two digits in the computer's bit order)
N No-number short integer (decimal two, the order in which the high is placed)
V no-number short integer (two-digit, descending order)
F single exact floating point number (in computer range)
D times the exact floating point number (based on the computer range)
X vacancy
X returns one digit.
@ Fill in the NULL character to the absolute position
Unpack function description:This function is used to extract the data of a bit string.
Syntax:Unpack (format, args +)
Parameter description
Format is required. The format used for packaging data.
Args + optional. Specify one or more parameters to be packaged.
The parameters are the same as those of pack.
Ord function description:Returns the acill code of the corresponding character.
Syntax:Ord ($ character );
Instance description:
The code is as follows:
// 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 characters
$ Str = (pack ("C *", "55", "56", "57 "));
Echo $ str, "=", strlen ($ str), "byte \ n ";
GetAscill ($ str );
// I character short integer 32-bit 4-byte 64-bit 8-byte
$ Str = (pack ("I", "100 "));
Echo $ str, "=", strlen ($ str), "byte \ n ";
GetAscill ($ str );
// The characters in seconds are short and shaping 2 bytes.
$ Str = (packs ("s", "100 "));
Echo $ str, "=", strlen ($ str), "byte \ n ";
GetAscill ($ str );
// The length of the l character is 4 bytes.
$ Str = (packs ("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 double precision floating point 8 bytes
$ Str = (pack ("DS", "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 ";
}
?>

Through the above example, we can see that the same string is stored in different formats and the number of bytes occupied is different. You can also see that saving characters in different formats can save storage space. And enable the unreadable encryption effect. Suddenly, the design database field type problem occurs if a field is only a 10-bit integer. We set the integer: 256*256*256*256 to 4 bytes. if it is set to 10 characters in length. It takes up to 10 bytes. The whole digestion space is twice. Setting the correct character type can greatly improve database performance. Oh, it's a bit out of question ......
Analysis of bytecode communication instances processed by php
The role of pack just mentioned: space saving and encryption format
The following describes the two instances and interface development requirements:
Parameter description
Username: 20 bytes in bytes
Password 10 bytes, character type
1-byte age, char-less
4-byte date of birth, integer (19800101)
Mailbox 50 bytes, string
The fields are separated by \ 0.
A. PACK packets
The code is as follows:
$ Code = array (
"Username" => array ("A20", "Zhang San adfb12 "),
"Pass" => array ("A10", "asdf * #1 "),
"Age" => array ("C", "23 "),
"Birthday" = & gt; 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); // Save the stream for easy reading

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

Because it is separated by "\ 0", the entire length is 89 bytes. Through the above output, some string outputs can be read, and others have become garbled. This is also the reason why I say it can be kept confidential.
B. Unpack
To unpack a package, you need to follow the packaging method to read the package. how long to read the package and the type of read to use must be the same as the packaging rules.
The code is as follows:
$ 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;
}
?>

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.