PHP Park, Unpark, Ord function usage (binary flow Interface application example) _php tips

Source: Internet
Author: User
Tags ord pack unpack
Park,unpark,ord These 3 functions, in our work, they are not much estimated. 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 Park,unpark,ord is powerful for binary byte processing. Let me introduce them each.

Introduction to the use of Park,unpark,ord functions

Park Function Description : This function is used to package data compression in place in the string.

Syntax : Pack (format,args+)

Parameters Describe
Format Necessary. Specify the format to use when wrapping the data.
args+ Optional. Specify one or more parameters to be wrapped.

Character Description
A Fills a 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 With number characters
C No number characters
S Short integer with number (16 bits, by computer's bit order)
S unsigned short integers (16 bits, by computer's bit order)
N Short-unsigned integers (16-bit, high in the latter order)
V Short-unsigned integers (16-bit, low in order of post)
I Integer with number (depending on the order and range of the computer)
I unsigned integers (in order and scope of the computer)
L Long integer with number (32 bits, by computer's bit order)
L unsigned long integers (32 bits, by computer's bit order)
N Short-unsigned integers (32-bit, high in the latter order)
V Short-unsigned integers (32-bit, low in order of post)
F Single precise floating-point number (depending on the scope of the computer)
D Times the exact floating-point number (depending on the range of the computer)
X Vacancy
X Pour back a
@ Fill in NULL characters to absolute position

unpark Function Description: This function is used to extract bits of string data

Syntax : Unpack (format,args+)

Parameters Describe
Format Necessary. 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 park.

Ord function Description: Returns the Acill code value of the corresponding character

Grammar : 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:
Parameters Describe
User name 20 bytes, character type
Password 10 bytes, Character type
Age 1-byte, no character char
Date of birth 4-byte, int (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", Parkbyarr ($code));
Echo $stream, strlen ($stream);


Copy Code code as follows:

File_put_contents ("C:/1.txt", $stream);//save stream for easy reading below

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

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 (Parkbyarr ($stream, $code));
function Parkbyarr ($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.