内容简介:这篇文章主要介绍了Python内建模块struct实例详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
本文研究的主要是 Python 内建模块struct的相关内容,具体如下。
Python中变量的类型只有列表、元祖、字典、集合等高级抽象类型,并没有像c中定义了位、字节、整型等底层初级类型。因为Python本来就是高级解释性语言,运行的时候都是经过翻译后再在底层运行。如何打通Python和其他语言之间的类型定义障碍,Python的内建模块struct完全解决了所有问题。
知识介绍:
在struct模块中最最常用的三个:
(1)struct.pack:用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。
(2)struct.unpack: 刚好与struct.pack相反,用于将字节流转换成python数据类型,该函数返回一个元组。
(3)struct.calcsize: 计算格式字符串所对应的结果的长度。
转换过程中遇到的格式操作:
| 格式符 | C语言类型 | Python类型 |
|---|---|---|
| x | pad byte | no value |
| c | char | string of length 1 |
| b | signed char | integer |
| B | unsigned char | integer |
| ? | _Bool | bool |
| 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 |
| Q | unsigned long long | long |
| f | float | float |
| d | double | float |
| s | char[] | string |
| p | char[] | string |
| P | void * | long |
实例详解:
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''测试struct模块'''
from struct import *
import array
def fun_calcsize():
print 'ci:',calcsize('ci')#计算格式占内存大小
print '@ci:',calcsize('@ci')
print '=ci:',calcsize('=ci')
print '>ci:',calcsize('>ci')
print '<ci:',calcsize('<ci')
print 'ic:',calcsize('ic')#计算格式占内存大小
print '@ic:',calcsize('@ic')
print '=ic:',calcsize('=ic')
print '>ic:',calcsize('>ic')
print '<ic:',calcsize('<ic')
def fun_pack(Format,msg = [0x11223344,0x55667788]):
result = pack(Format,*msg)
print 'pack'.ljust(10),str(type(result)).ljust(20),
for i in result:
print hex(ord(i)), # ord把ASCII码表中的字符转换成对应的整形,hex将数值转化为十六进制
print
result = unpack(Format,result)
print 'unpack'.ljust(10),str(type(result)).ljust(20),
for i in result:
print hex(i),
print
def fun_pack_into(Format,msg = [0x11223344,0x55667788]):
r = array.array('c',' '*8)#大小为8的可变缓冲区,writable buffer
result = pack_into(Format,r,0,*msg)
print 'pack_into'.ljust(10),str(type(result)).ljust(20),
for i in r.tostring():
print hex(ord(i)),
print
result = unpack_from(Format,r,0)
print 'pack_from'.ljust(10),str(type(result)).ljust(20),
for i in result:
print hex(i),
print
def IsBig_Endian():
'''判断本机为大/小端'''
a = 0x12345678
result = pack('i',a)#此时result就是一个string字符串,字符串按字节同a的二进制存储内容相同。
if hex(ord(result[0])) == '0x78':
print '本机为小端'
else:
print '本机为大端'
def test():
a = '1234'
for i in a:
print '字符%s的二进制:'%i,hex(ord(i))#字符对应ascii码表中对应整数的十六进制
'''
不用unpack()返回的数据也是可以使用pack()函数的,只要解包的字符串符合解包格式即可,
pack()会按照解包格式将字符串在内存中的二进制重新解释(说的感觉不太好...,见下例)
'''
print '大端:',hex(unpack('>i',a)[0])#因为pack返回的是元组,即使只有一个元素也是元组的形式
print '小端:',hex(unpack('<i',a)[0])
if __name__ == "__main__":
print '判断本机是否为大小端?',
IsBig_Endian()
fun_calcsize()
print '大端:'
Format = ">ii"
fun_pack(Format)
fun_pack_into(Format)
print '小端:'
Format = "<ii"
fun_pack(Format)
fun_pack_into(Format)
print 'test'
test()
'''
result:
判断本机是否为大小端? 本机为小端
ci: 8
@ci: 8
=ci: 5
>ci: 5
<ci: 5
ic: 5
@ic: 5
=ic: 5
>ic: 5
<ic: 5
大端:
pack <type 'str'> 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
unpack <type 'tuple'> 0x11223344 0x55667788
pack_into <type 'NoneType'> 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
pack_from <type 'tuple'> 0x11223344 0x55667788
小端:
pack <type 'str'> 0x44 0x33 0x22 0x11 0x88 0x77 0x66 0x55
unpack <type 'tuple'> 0x11223344 0x55667788
pack_into <type 'NoneType'> 0x44 0x33 0x22 0x11 0x88 0x77 0x66 0x55
pack_from <type 'tuple'> 0x11223344 0x55667788
test
字符1的二进制: 0x31
字符2的二进制: 0x32
字符3的二进制: 0x33
字符4的二进制: 0x34
大端:0x31323334
小端:0x34333231
'''
本实例所用Python软件环境:win10+anaconda3+pycharm,Python版本:3.6
总结
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to Probability Theory and Its Applications
William Feller / Wiley / 1991-1-1 / USD 120.00
Major changes in this edition include the substitution of probabilistic arguments for combinatorial artifices, and the addition of new sections on branching processes, Markov chains, and the De Moivre......一起来看看 《An Introduction to Probability Theory and Its Applications》 这本书的介绍吧!