How can we convert data into this form?

Source: Internet
Author: User
Tags example of manual
How can we convert data into this form? It seems to be binary. What function does php use to convert a string or array to this?


Reply to discussion (solution)

Pack ()
The reverse direction is unpack ()


Pack ()
The reverse direction is unpack ()

It seems like a pack, but this function has many parameters. I have tried it and the running results cannot display similar data structures.

If only pack can be used, that's strange.

Ask LZ if you want to add it upstairs
You won't directly look at pack () results, right?
The result after pack () is a byte, which must be written to the file in wb mode and opened in a hex-supported editor.
And convert different strings with different parameters (this string must be considered in bytes). study the meaning of those parameters.

Ask LZ if you want to add it upstairs
You won't directly look at pack () results, right?
The result after pack () is a byte, which must be written to the file in wb mode and opened in a hex-supported editor.
And convert different strings with different parameters (this string must be considered in bytes). study the meaning of those parameters.

Convert a string to binary. Is the parameter a correct? Or upper-case?

The string is binary.
A indicates that in the specified length, the insufficient part is filled with null (0x00 ).
A indicates that in the specified length, the insufficient part is filled with spaces (0x20 ).
It makes no sense if no length is specified.

$s = 'CSDN(www.csdn.net)';for($i=0; $i
 
  ';}
 
43 53 44 4e 28 77 77 2e 63 73 64 6e 2e 6e 65
74 29

The string is binary.
A indicates that in the specified length, the insufficient part is filled with null (0x00 ).
A indicates that in the specified length, the insufficient part is filled with spaces (0x20 ).
It makes no sense if no length is specified.

$s = 'CSDN(www.csdn.net)';for($i=0; $i
 
  ';}
 
43 53 44 4e 28 77 77 2e 63 73 64 6e 2e 6e 65
74 29

It feels a little complicated. why do we have to specify the length? Why is it meaningless without specifying the length?

The fixed length written by Xu refers to the length required by the format parameter, rather than the length of the string (input ).
Example of manual
Example #1 pack () example
$ Binarydata = pack ("nvc *", 0x1234, 0x5678, 65, 66 );
?>
The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
Because the n and v parameters are 16 bits and c parameters are 8 bits, the following parameters 0 x are followed by the n parameter, 0x5678 is followed by the v parameter, and 65 is followed by the c parameter, * indicates that the c parameter will be followed, so 66 is also output according to the c parameter.
In other words, the * sign in the format parameter can be used for an input with an indefinite length.

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.