프로그래밍/Python
[Python] struct 정리
1q
2016. 1. 15. 17:58
참고 : https://docs.python.org/2/library/struct.html
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로 리턴
Character | Byte order | Size | Alignment |
---|---|---|---|
@ | native | native | native |
= | native | standard | none |
< | little-endian | standard | none |
> | big-endian | standard | none |
! | network (= big-endian) | standard | none |
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) |