Python
python struct module
Realsung
2019. 11. 21. 00:53
Python 문자열로 표현된 C structs와 Python Value 간의 변환
파일에 Binary Data를 저장하거나 Network Connection 시 사용
struct.pack(fmt, v1, v2,...)
- 지정된 Format에 따라 v1, v2의 value를 Pack을 수행하며 그 결과는 String으로 리턴
struct.unpack(fmt, string)
- 지정된 Format에 따라 String을 Unpack을 수행하며 그 결과는 Tuple로 리턴
CharacterByte orderSizeAlignment
@ | native | native | native |
= | native | standard | none |
< | little-endian | standard | none |
> | big-endian | standard | none |
! | network (= big-endian) | standard | none |
FormatC TypePython typeStandard sizeNotes
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) |
ex) struct.pack('<B',i)