How can I convert the data into this form?
This seems to be binary. what function does PHP use to convert a string or an array to this?
Share to:
------Solution--------------------
Pack ()
Reverse is unpack ()
------Solution--------------------
If you can do it with a pack, it's weird.
------Solution--------------------
The string is the binary
A indicates that the insufficient portion is filled with null (0x00) in the specified length
A means that in the specified length, the insufficient portion is filled with a space (0x20)
If you don't specify a length, it doesn't make any sense.
$s = ' CSDN (www.csdn.net) ';
for ($i =0; $i
printf ('%02x ', ord ($s {$i}));
if (($i + 1)%16 = = 0) echo '
';
}
4e (2e) 6e 2e 6e 65
74 29
------Solution--------------------
Lao Xu's length refers to the length of the format parameter, not the string (input).
Examples of manuals such as
Example #1 Pack () Example
$binarydata = Pack ("nvc*", 0x1234, 0x5678, 65, 66);
?>
The resulting binary string would be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
Since the parameters N and v are 16 bits, and C is 8 bits, so the following parameters 0x1234 by the n parameter, 0x5678 by the V parameter, 65 by the C parameter, the * number indicates the subsequent continuation of the C parameter, so 66 also press the C parameter output
In other words, the * number in the format parameter can be used for an indefinite length of input.