参考文档:
http://docs.python.org/2/library/struct.html
http://blog.163.com/ws__fyy/blog/static/12243381720123181013666/
http://blog.163.com/kongdelu2009@yeah/blog/static/1119952072009102562126194/
# -*- coding: utf-8 -*- import struct import sys import os ''' 1. Byte order, Size, Alignment ------------------------------------------------------------- Character Byte order Size Alignment ------------------------------------------------------------- @ native native native ==> default = native standard none < little-endian standard none > big-endian standard none ! network (= big-endian) standard none ------------------------------------------------------------- 1). byte order: Native byte order is big-endian or little-endian, depending on the host system. ①Intel X86 / AMD64(x86-64) ==> little-endian ②Motorola 68000 / PowerPC G5 ==> big-endian ③ARM / Intel Itanium feature switchable endianness (bi-endian) 2). size: ①Native size are determined using the C compiler's sizeof expression. ②Standard size depends only on the 'format character' 3). alignment: 2. format character Format C Type Python type Standard size x pad byte no value c char string of length 1 1 b signed char integer 1 B unsigned char integer 1 ? _Bool bool 1 h short integer 2 H unsigned short integer 2 i int integer 4 I unsigned int integer 4 l long integer 4 L unsigned long integer 4 q long long integer 8 Q unsigned long long integer 8 f float float 4 d double float 8 s char[] string p char[] string P void * integer ''' #检查大端小端 print sys.byteorder s='abcde' a=20 b=400 ''' 3. python obj-->pack-->str ''' packed_str = struct.pack('< 5s2i',s,a,b) #< : little endian, standard side, none alignment # whitespaces are ignored. #5s : 5 is the "size" of the string #2i : 2 is the "repeat count" #print len(s) print struct.calcsize('< 5s2i') print repr(s) print type(s) ''' 4. str-->pack-->python obj ''' (s, a, b) = struct.unpack('<5s2i', packed_str) print s print a print b
相关推荐
将c结构直接转换成python.struct需要的东西
python库。 资源全名:struct-model-python-0.1.0.tar.gz
使用python的struct库实现IP地址计算,根据一个int值
资源分类:Python库 所属语言:Python 资源全名:jsonstruct-0.2a1.linux-x86_64.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
" "struct模块的内容不多,也不是太难,下面对其中最常用的方法进行介绍: " "最简单的图解: " "3s代表3个字节的string。每个s是一个Byte。(汉字2个)。 " "注:'10s' means a single 10-byte string (py
使用python的struct库实现IP地址计算,根据一个int值
python 和 C语言进行网络通信,而C语言经常是采用struct ,所以和python交互需要进行解析和重构struct,因此python 实现struct模块,文档右举例和使用方法,让你快速上手。
Python使用struct处理二进制的实例详解 有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最...
python的struct模块.docx
主要介绍了Python中的struct模块,代码基于Python2.x版本,需要的朋友可以参考下
但当Python需要通过网络与其他的平台进行交互的时候,必须考虑到将这些数据类型与其他平台或语言之间的类型进行互相转换问题。打个比方:C++写的客户端发送一个int型(4字节)变量的数据到Python写的服务器,Python...
本文研究的主要是Python内建模块struct的相关内容,具体如下。 Python中变量的类型只有列表、元祖、字典、集合等高级抽象类型,并没有像c中定义了位、字节、整型等底层初级类型。因为Python本来就是高级解释性语言,...
python提供了一个struct模块来提供转换。下面就介绍这个模块中的几个方法。 struct.pack(): struct.pack用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为...
树莓派I2C通信,使用python的UPD网络远程控制树莓派i2c发送指令和读取数据指令,里面包含了3个i2c子设备
struct.nim:Nim的类似Python的“ struct”
今天小编就为大家分享一篇python中struct模块之字节型数据的处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
# 改变结构体成员的字节对齐 ## 例子 #include typedef struct { char a; int b; } ST_A;
主要介绍了Python标准库笔记struct模块的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
pycascading, 用于级联的python 包装器 PyCascadingPyCascading是用于级联的python 封装器。 你可以从 python 控制完整的数据处理工作流。使用 python 运算符构建管道用户定义的函数是用 python 编写将任意上下文...