標籤:... compile struct not convert ret object signed xpl
1 import struct # a module 2 3 4 # Functions to convert between Python values and C structs. 5 # Python bytes objects are used to hold the data representing the C struct 6 # and also as format strings (explained below) to describe the layout of data in the C struct. 7 # @: native order, size & alignment (default) 8 # =: native order, std. size & alignment 9 # <: little-endian, std. size & alignment10 # >: big-endian, std. size & alignment11 # !: same as >12 13 # x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;14 # ?: _Bool (requires C99; if not available, char is used instead)15 # h:short; H:unsigned short; i:int; I:unsigned int;16 # l:long; L:unsigned long; f:float; d:double; e:half-float.Special cases (preceding decimal count indicates length):17 # s:string (array of char); p: pascal string (with count byte).Special cases (only available in native format):18 # n:ssize_t; N:size_t;19 # P:an integer type that is wide enough to hold a pointer.Special case (not in native mode unless ‘long long‘ in platform C):20 # q:long long; Q:unsigned long long21 # class Struct(object) Struct(fmt) --> compiled struct object22 # Return a new Struct object which writes and reads binary data according to the format string fmt23 24 # iter_unpack(...) S.iter_unpack(buffer) -> iterator(v1, v2, ...)25 #26 # pack_into(...) S.pack_into(buffer, offset, v1, v2, ...)27 # unpack_from(...) S.unpack_from(buffer, offset=0) -> (v1, v2, ...)28 #29 # pack(...) S.pack(v1, v2, ...) -> bytes30 # unpack(...) S.unpack(buffer) -> (v1, v2, ...)31 32 33 import struct34 35 36 # 1、struct.pack37 a = 2038 b = 40039 40 str_ = struct.pack(">ii", a, b) # 轉換後的str雖然是字串類型,但相當於其他語言中的位元組流(位元組數組),可以在網路上傳輸41 print(‘length:‘, len(str_))42 print(‘type:‘, type(str_))43 print(str_)44 print(repr(str_))45 46 # length: 847 # type: <class ‘bytes‘>48 # b‘\x00\x00\x00\x14\x00\x00\x01\x90‘49 # b‘\x00\x00\x00\x14\x00\x00\x01\x90‘50 51 52 str_ = struct.pack("<ii", a, b) # 轉換後的str雖然是字串類型,但相當於其他語言中的位元組流(位元組數組),可以在網路上傳輸53 print(‘length:‘, len(str_))54 print(‘type:‘, type(str_))55 print(str_)56 print(repr(str_))57 58 # length: 859 # type: <class ‘bytes‘>60 # b‘\x14\x00\x00\x00\x90\x01\x00\x00‘61 # b‘\x14\x00\x00\x00\x90\x01\x00\x00‘62 63 64 # 2、struct.unpack65 a1, a2 = struct.unpack("<ii", str_) # 返回元祖66 print(a1, a2) # 20,40067 68 69 # 3、struct.calcsize用於計算格式字串所對應的結果的長度70 print(struct.calcsize(‘>ii‘)) # 871 72 73 # 4、struct.pack_into, struct.unpack_from74 from ctypes import create_string_buffer75 76 buf = create_string_buffer(12)77 print(repr(buf.raw)) # b‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘78 79 struct.pack_into(">iii", buf, 0, 1, 2, -1) # 0是offset80 print(repr(buf.raw)) # b‘\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff‘81 82 print(struct.unpack_from(‘>iii‘, buf, 0)) # (1, 2, -1)83 84 # 5、struct.iter_unpack85 ret = struct.iter_unpack(‘>iii‘, buf)86 print(ret) # <unpack_iterator object at 0x0000000001D02E18>87 for r in ret:88 print(r) # (1, 2, -1)89 90 91 # 6、練習92 print(struct.pack(‘ci‘, b‘*‘, 0x12131415)) # b‘*\x00\x00\x00\x15\x14\x13\x12‘93 print(struct.pack(‘ic‘, 0x12131415, b‘*‘)) # b‘\x15\x14\x13\x12*‘94 95 record = b‘raymond \x32\x12\x08\x01\x08‘96 print(struct.unpack(‘<10sHHb‘, record)) # (b‘raymond ‘, 4658, 264, 8)
| 字元 |
位元組順序 |
大小 |
對齊 |
| @ |
本機 |
本機 |
本機 |
| = |
本機 |
標準 |
無 |
| < |
小端 |
標準 |
無 |
| > |
大端 |
標準 |
無 |
| ! |
網路(等於大端) |
標準 |
無 |
| 格式 |
C類型 |
Python類型 |
標準大小 |
提示 |
| x |
填充位元組 |
沒有值 |
|
|
| c |
char |
長度為1的位元組 |
1 |
|
| b |
signed char |
integer |
1 |
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 |
| n |
ssize_t |
integer |
|
4 |
| N |
size_t |
integet |
|
4 |
| e |
7 |
float |
2 |
5 |
| f |
float |
float |
4 |
5 |
| d |
double |
float |
8 |
5 |
| s |
char[] |
bytes |
|
|
| p |
char[] |
bytes |
|
|
| P |
void * |
integer |
|
6 |
python struct模組