python struct pack unpack
python的struct模块
FORMAT | C TYPE | PYTHON TYPE | STANDARD SIZE | NOTES |
---|---|---|---|---|
x | pad byte | no value | - | - |
c | char | string of length 1 | 1 | - |
b | signed char | integer | 1 | (3) |
B | unsigned char | integer | 1 | (3) |
? | _Bool | bool | 1 | (1) |
h | short | integer | 2 | (3) |
H | unsigned short | integer | 2 | (3) |
i | int | integer | 4 | (3) |
I | unsigned int | integer | 4 | (3) |
l | long integer | 4 | (3) | - |
L | unsigned long | integer | 4 | (3) |
q | long long | integer | 8 | (2), (3) |
Q | unsigned long long | integer | 8 | (2), (3) |
f | float | float | 4 | (4) |
d | double | float | 8 | (4) |
s | char[] | string | - | - |
p | char[] | string | - | - |
P | void * | integer | - | (5), (3) |
import struct class Header: def __init__(self): self.id = 1 #int H self.age=2 #short h self.weight=0.0 #float f self.count=0 #int i self.totalS = 0.0 #double head1 = Header() head1.id = 12 head1.age = 125 head1.weight = 3302.98 head1.count = 1202 head1.totalS = 53.089 ss = struct.pack("!ihfid", head1.id, head1.age, head1.weight, head1.count,head1.totalS) print(ss) id, age, weight, count ,totalS = struct.unpack("!ihfid", ss) print(id, age, weight, count, totalS)
(wind_2021) F:\PytorchProject\project_liao_20220523>python test_pack_unpack.py b'\x00\x00\x00\x0c\x00}ENo\xae\x00\x00\x04\xb2@J\x8bdZ\x1c\xac\x08' 12 125 3302.97998046875 1202 53.089 (wind_2021) F:\PytorchProject\project_liao_20220523>
########################
QQ 3087438119