PHP pack, unpack, Ord function using method (binary stream Interface application example) go

Source: Internet
Author: User
Tags ord unpack vars

PHP pack, unpack, Ord function using method (binary stream Interface application example) Blog Category:
    • Php
PHP binary Packunpackord in the work, I also gradually learned that Pack,unpack,ord is powerful for binary byte processing. Let me introduce them to you. In our work, there is not much estimation of their use. I am in a recent job, because the communication needs to use the binary stream, and then the interface is received in PHP. At that time, a lot of information was consulted. Because they are really less used, they are seldom used by friends in their work. At work, I also learned that Pack,unpack,ord is powerful for binary byte processing. Let me introduce them to you.
Pacrk function Description: This function is used to compress data into a string in place.


Syntax: Pack (format,args+)

Parameter description
Format required. Specifies the format to use when wrapping the data.
Args+ is optional. Specifies one or more parameters that are packaged.


Character description
A fills the string blank with a NULL character
A fills the string blanks with space characters (spaces)
H 16 string, low in front
H 16 string, high in front
C with number characters
C No number character
S has a short integer (16 bits, depending on the computer's bit order)
S no-number short integer (16 bits, depending on the computer's bit order)
n unsigned short integers (16-bit, high-order in the back)
V Short Integer (16 bits, low in the back order)
I number integers (depending on the computer's order and range)
I no-number integers (depending on the computer's order and range)
L number Long Integer (32 bits, depending on the computer's bit order)
L unsigned long integers (32 bits, depending on the computer's bit order)
N unsigned short integers (32-bit, high-order in the back)
V short Integer (32 bits, low in the back order)
F Single precision floating point number (depending on the range of the computer)
D-times accurate floating point number (depending on the computer's range)
X vacancy
X Rewind One
@ Fill in NULL characters to absolute position


Unpack function Description: This function is used to extract the data of the bit string

Syntax: Unpack (format,args+)

Parameter description
Format required. Specifies the format to use when wrapping the data.
Args+ is optional. Specifies one or more parameters that are packaged.

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:
PHP code
  1. A character
  2. $str = (Pack ( "A *", "China"));
  3. Echo $str, "=", strlen ( $str), "byte \ n";
  4. Getascill ( $str);
  5. echo ' <br/> ';
  6. H character
  7. $str = (Pack ( "h*", "Fffe"));
  8. Echo $str, "=", strlen ( $str), "byte \ n";
  9. Getascill ( $str);
  10. echo ' <br/> ';
  11. C character
  12. $str = (Pack ( "c*", "n", "57") ;
  13. Echo $str, "=", strlen ( $str), "byte \ n";
  14. Getascill ( $str);
  15. echo ' <br/> ';
  16. I-character short-shaped 32-bit 4-byte 64-bit 8-byte
  17. $str = (Pack ( "i", "100"));
  18. Echo $str, "=", strlen ( $str), "byte \ n";
  19. Getascill ( $str);
  20. echo ' <br/> ';
  21. s character short shaping 2 bytes
  22. $str = (Pack ( "s", "100"));
  23. Echo $str, "=", strlen ( $str), "byte \ n";
  24. Getascill ( $str);
  25. echo ' <br/> ';
  26. L character-length shaping 4 bytes
  27. $str = (Pack ( "L", "100"));
  28. Echo $str, "=", strlen ( $str), "byte \ n";
  29. Getascill ( $str);
  30. echo ' <br/> ';
  31. F-character single-precision floating-point 4 bytes
  32. $str = (Pack ( "F", "100"));
  33. Echo $str, "=", strlen ( $str), "byte \ n";
  34. Getascill ( $str);
  35. echo ' <br/> ';
  36. D-character double-precision floating-point 8 bytes
  37. $str = (Pack ( "D", "100"));
  38. Echo $str, "=", strlen ( $str), "byte \ n";
  39. Getascill ( $str);
  40. echo ' <br/> ';
  41. function Getascill ($str) {
  42. $arr = str_split ( $str);
  43. foreach ( $arr as $v) {
  44. echo $v, "=", Ord ( $v), "\ n";
  45. }
  46. echo "=============\r\n\r\n";
  47. }


With the above example, we can see that the same string, stored in a different format, consumes a different number of bytes. It can also be seen here that saving characters in different formats can save space for storage. and start the unreadable encryption effect. Suddenly think of a bit, Design database field type problems, if a field is just: 10-bit length integer type. We set it to reshape: 256*256*256*256 is 4 bytes, if set to 10 length string. That accounts for 10 bytes. The whole digestive space is twice times the size. Setting the correct character type is a great help in improving database performance. Oh, a little off-topic ...




A case analysis of PHP processing byte code communication
Just said pack function: space-saving, encrypted format

Here are 2 examples to illustrate, interface development requirements:


Parameter description
User name 20 bytes, character type
Password 10 bytes, character type
Age 1 Bytes, no character char type
Date of birth 4 bytes, integer (19800101)
Mailbox 50 Bytes, string
Between fields: "\" split
A, pack Packaging

PHP code
  1. $code =Array (
  2. "username" = =Array ("A20","Zhang San adfb12"),
  3. "Pass" =Array ("A10","asdf* #1"),
  4. "Age" = =Array ("C", "All"),
  5. "Birthday" = =Array ("I","19900101"),
  6. "Email" = =Array ("A50","[email protected]"));
  7. $stream =join ("n", Packbyarr ($code));
  8. Echo $stream,strlen ($stream);
  9. File_put_contents ("C:/1.txt",$stream); //Save the stream for easy reading below.
  10. function Packbyarr ($arr) {
  11. $ATARR =Array ();
  12. foreach ($arr as $k = =$v) {
  13. $ATARR []=pack ($v [0],$v [1]);
  14. }
  15. return $ATARR;
  16. }
  17. function Getascill ($str) {
  18. $arr =str_split ($str);
  19. foreach ($arr as $v) {
  20. echo $v,"=", Ord ($v),"\ n";
  21. }
  22. }


The entire length is 89 bytes, because it is split with "\". With the above output, some string output can be read, the others have become garbled. This is what I said can be kept secret effect reason.

B, unpack unpacking
The unpacking needs to be read in a packaged manner, the length of the read, what type to read, and the same as the packing rules.

PHP code
  1. $code =Array (
  2. "username" = =Array ("A20"),
  3. "Pass" =Array ("A10"),
  4. "Age" = =Array ("C"),
  5. "Birthday" = =Array ("I"),
  6. "Email" = =Array ("A50"));
  7. $stream =file_get_contents ("C:/1.txt");
  8. Var_dump (Packbyarr ($stream,$code));
  9. function Packbyarr ($str,$code) {
  10. $ARR =explode ("n",$str);
  11. $ATARR =Array ();
  12. $i = 0;
  13. foreach ($code as $k = =$v) {
  14. $ATARR [$k]=unpack ($v [0],$Arr [$i]);
  15. $i + +;
  16. }
  17. return $ATARR;
  18. }



Citation: http://randomclan.blog.163.com/blog/static/1453009820125454418912/

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.