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? Share to: how can I convert data in this form?
It seems to be binary. What function does php use to convert a string or array to this?
Share:
------ Solution --------------------
Pack ()
The reverse direction is unpack ()
------ Solution --------------------
If only pack can be used, that's strange.
------ Solution --------------------
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
printf('%02x ', ord($s{$i}));
if(($i+1)%16 == 0) echo '
';
}
43 53 44 4e 28 77 77 2e 63 73 64 6e 2e 6e 65
74 29
------ Solution --------------------
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.