Python的struct模块
2011-03-01 14:34
591 查看
struct模块提供将二进制数据转换为结构化数据或相反的功能,它定义了以下函数和异常:
exception struct.error
struct.pack(fmt, v1, v2, …)
返回一个string,string由v1,v2…经过给出的格式fmt组成,参数的个数有和类型要和给出的格式一一对应
struct.pack_into(fmt, buffer, offset, v1, v2, …)
按照格式fmt将v1, v2 …打包,并从buffer的偏移量offset开始写进buffer中
struct.unpack(fmt, string)
以给定的格式fmt来将string解包,结果是个元组(tuple),即使结果只有一个值。fmt所要求的长度必须和string的长度相等
struct.unpack_from(fmt, buffer[, offset=0])
以给定的格式fmt来将buffer解包,结果是个元组
struct.calcsize(fmt)
返回给出格式对应的结构的长度
格式如下表:
C Type列指Foramt列字母所代表的打包数据
Python列指打包数据在python里面表示的类型
说明:
格式字符可以以一个数字作为前缀n,表示n个连续的该格式,例如4h表示hhhh
在格式(formats)中间的空白字符将被忽略
s格式的计数n代表n个字节长度的string,例如’10s’代表10-byte string
format的第一个字符可以标示为字节序、对齐方式、数据类型大小等,如下表:
如果没有第一个字符,则默认为@
native byteorder表示字节序取决于本机系统
native size and alignment表示数据类型大小和对齐方式与c编译器相关
standard size and alignment表示不进行任何对齐,short为2字节,int和long为4字节,long
long为8字节,float和 double是32bit和64bit的IEEE的浮点数,_Bool为1字节
转帖自:http://www.fengyj.net/blog/?p=487
exception struct.error
struct.pack(fmt, v1, v2, …)
返回一个string,string由v1,v2…经过给出的格式fmt组成,参数的个数有和类型要和给出的格式一一对应
struct.pack_into(fmt, buffer, offset, v1, v2, …)
按照格式fmt将v1, v2 …打包,并从buffer的偏移量offset开始写进buffer中
struct.unpack(fmt, string)
以给定的格式fmt来将string解包,结果是个元组(tuple),即使结果只有一个值。fmt所要求的长度必须和string的长度相等
struct.unpack_from(fmt, buffer[, offset=0])
以给定的格式fmt来将buffer解包,结果是个元组
struct.calcsize(fmt)
返回给出格式对应的结构的长度
格式如下表:
C Type列指Foramt列字母所代表的打包数据
Python列指打包数据在python里面表示的类型
Format | C Type | Python | Notes |
---|---|---|---|
x | pad byte | no value | |
c | char | string of length 1 | |
b | signed char | integer | |
B | unsigned char | integer | |
? | _Bool | bool | (1) |
h | short | integer | |
H | unsigned short | integer | |
i | int | integer | |
I | unsigned int | integer or long | |
l | long | integer | |
L | unsigned long | long | |
q | long long | long | (2) |
Q | unsigned long long | long | (2) |
f | float | float | |
d | double | float | |
s | char[] | string | |
p | char[] | string | |
P | void * | long |
格式字符可以以一个数字作为前缀n,表示n个连续的该格式,例如4h表示hhhh
在格式(formats)中间的空白字符将被忽略
s格式的计数n代表n个字节长度的string,例如’10s’代表10-byte string
format的第一个字符可以标示为字节序、对齐方式、数据类型大小等,如下表:
Character | Byte order | Size and alignment |
---|---|---|
@ | native | native |
= | native | standard |
< | little-endian | standard |
> | big-endian | standard |
! | network (= big-endian) | standard |
native byteorder表示字节序取决于本机系统
native size and alignment表示数据类型大小和对齐方式与c编译器相关
standard size and alignment表示不进行任何对齐,short为2字节,int和long为4字节,long
long为8字节,float和 double是32bit和64bit的IEEE的浮点数,_Bool为1字节
转帖自:http://www.fengyj.net/blog/?p=487
相关文章推荐
- 浅析Python中的struct模块
- Python模块学习 ---- struct 数据格式转换
- python:struct模块,二进制数据结构
- 浅析Python中的struct模块(python网络编程)
- python中struct模块及packet和unpacket
- [Python 实战] - No.11 Python Struct 模块使用
- Python中struct模块的使用
- Python之struct模块(处理二进制)
- python中使用struct模块处理二进制数据
- python中struct模块及packet和unpacket
- Python模块--struct 数据格式转换
- Python struct模块解析
- python-Struct模块
- Python标准库笔记struct模块的使用
- Python模块学习 ---- struct 数据格式转换
- Python之struct模块浅谈
- Python学习——struct模块的pack、unpack示例
- Python学习——struct模块的pack、unpack示例
- Python模块——struct(字节流,组包拆包实现)
- Python模块学习 ---- struct 数据格式转换