Php park, unpark, ord function usage (binary stream interface application instance)

Source: Internet
Author: User

The three functions park, unpark, and ord are not used much in our work. 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 park, unpark, and ord are powerful in processing binary bytes. Next I will introduce them one by one.

Introduction to using park, unpark, and ord Functions

Park Function Description: This function is used to compress and package data into strings.

 

Syntax: Pack (format, args +)

Parameters Description
Format Required. The format used for packaging data.
Args + Optional. Specify one or more parameters to be packaged.

 

Character Description
A Fill the blank string with NULL characters
A Fill the blank string with SPACE characters
H Hexadecimal string, before low
H A hexadecimal string that is in front of a high position.
C Character with signs
C No. Character
S Short INTEGER (Sixteen digits, in the order of digits)
S No. Short INTEGER (Sixteen digits, in the computer's bit order)
N No. Short INTEGER (sixteen bits, in the descending order)
V No. Short INTEGER (sixteen bits, the order in which the low position is placed)
I An integer with numbers (in computer order and range)
I No-number INTEGER (in computer order and range)
L An integer with numbers (two digits of numbers, in the order of digits)
L No. Long Integer (two digits in the computer's bit order)
N No-number short INTEGER (two-digit, descending order)
V No. Short INTEGER (decimal two, descending order)
F Single exact floating point number (based on the computer range)
D Exact floating point number (in the computer range)
X Vacant Space
X Returns one digit.
@ Enter NULL characters to the absolute position

 

Unpark function description:This function is used to extract the data of a bit string.

Syntax: Unpack (format, args +)

Parameters Description
Format Required. The format used for packaging data.
Args + Optional. Specify one or more parameters to be packaged.

The parameter is the same as that of park.

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

Syntax: Ord ($ character );

Instance description:
Copy codeThe Code is 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 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:

Parameters Description
User Name 20 bytes, bytes type
Password 10 bytes, bytes type
Age 1 byte, char-less
Date of birth 4 bytes, INTEGER (19800101)
Email 50 bytes, string
The fields are separated by \ 0.
A. PACK packets
Copy codeThe Code is as follows:
<? Php
$ 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", parkByArr ($ code ));
Echo $ stream, strlen ($ stream );


Copy codeThe Code is as follows:
File_put_contents ("c:/1.txt", $ stream); // Save the stream for easy reading

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 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.
Copy codeThe Code is 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 (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;
}

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.