罗宁的博客

php和python的unpack对应的char类型

Php unpack

1
2
3
4
5
6
7
8
unpack — Unpack data from binary string
说明
array unpack ( string $format , string $data )
Unpacks from a binary string into an array according to the given format.
The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.

Python unpack

1
2
struct.unpack(fmt, buffer)
Unpack from the buffer buffer (presumably packed by pack(fmt, ...)) according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format, as reflected by calcsize().
1
2
3
4
5
//php a1 a2 a3 为$res的key值
$res = unpack("a3a1/aa2/a12a3/a8a4/aa5", $medInfo);
//python
format1 = '3s1s12s8s1s';
(a1, a2, a3, a4, a5, last) = struct.unpack(format1, filedata)