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

Source: Internet
Author: User
At work, I gradually learned that park, unpark, and ord are powerful in processing binary bytes. Next I will introduce them one by one.

At work, I gradually learned that park, unpark, and ord are powerful in processing binary bytes. Next I will introduce them one by one.

The three functions park, unpark, and ord are not used much in our work. In my recent job, the Hong Kong virtual host used binary stream for communication, and then the interface was received in php. At that time, I checked a lot of materials. Because they are actually used less, server space, U.S. space, and very 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

Description of the park function: 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

Unpark 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 parameter is the same as that of park.

Ord Function Description: return the acill code value 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", parkByArr ($ code ));
Echo $ stream, strlen ($ stream );



The 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.

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 (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.