字节流与数据类型的相互转换---使用struct模块
字节流与数据类型的相互转换---使用struct模块
http://blog.csdn.net/Sunboy_2050/article/details/5974029
Python是一门非常简洁的语言,对于数据类型的表示,不像其他语言预定义了许多类型(如:在C#中,光整型就定义了8种)
它只定义了六种基本类型:字符串,整数,浮点数,元组(set),列表(array),字典(key/value)
通过这六种数据类型,我们可以完成大部分工作。但当Python需要通过网络与其他的平台进行交互的时候,必须考虑到将这些数据类型与其他平台或语言之间的类型进行互相转换问题。打个比方:C++写的客户端发送一个int型(4字节)变量的数据到Python写的服务器,Python接收到表示这个整数的4个字节数据,怎么解析成Python认识的整数呢? Python的标准模块struct就用来解决这个问题。
struct模块的内容不多,也不是太难,下面对其中最常用的方法进行介绍:
1、 struct.pack struct.pack用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。其函数原型为:struct.pack(fmt, v1, v2, ...),参数fmt是格式字符串,关于格式字符串的相关信息在下面有所介绍。v1, v2, ...表示要转换的python值。下面的例子将两个整数转换为字符串(字节流):
- #!/usr/bin/env python
- #encoding: utf8
- import sys
- reload(sys)
- sys.setdefaultencoding("utf-8")
- import struct
- a = 20
- b = 400
- str = struct.pack("ii", a, b)
- print 'length: ', len(str) # length: 8
- print str # 乱码:
- print repr(str) # '\x14\x00\x00\x00\x90\x01\x00\x00'
格式符"i"表示转换为int,'ii'表示有两个int变量。
进行转换后的结果长度为8个字节(int类型占用4个字节,两个int为8个字节)
可以看到输出的结果是乱码,因为结果是二进制数据,所以显示为乱码。
可以使用python的内置函数repr来获取可识别的字符串,其中十六进制的0x00000014, 0x00001009分别表示20和400。
2、 struct.unpack struct.unpack做的工作刚好与struct.pack相反,用于将字节流转换成python数据类型。它的函数原型为:struct.unpack(fmt, string),该函数返回一个元组。
下面是一个简单的例子:
- #encoding: utf8
- import sys
- reload(sys)
- sys.setdefaultencoding("utf-8")
- import struct
- a = 20
- b = 400
- # pack
- str = struct.pack("ii", a, b)
- print 'length: ', len(str) # length: 8
- print str # 乱码:
- print repr(str) # '\x14\x00\x00\x00\x90\x01\x00\x00'
- # unpack
- str2 = struct.unpack("ii", str)
- print 'length: ', len(str2) # length: 2
- print str2 # (20, 400)
- print repr(str2) # (20, 400)
3、 struct.calcsize struct.calcsize用于计算格式字符串所对应的结果的长度,如:struct.calcsize('ii'),返回8。因为两个int类型所占用的长度是8个字节。
- import struct
- print "len: ", struct.calcsize('i') # len: 4
- print "len: ", struct.calcsize('ii') # len: 8
- print "len: ", struct.calcsize('f') # len: 4
- print "len: ", struct.calcsize('ff') # len: 8
- print "len: ", struct.calcsize('s') # len: 1
- print "len: ", struct.calcsize('ss') # len: 2
- print "len: ", struct.calcsize('d') # len: 8
- print "len: ", struct.calcsize('dd') # len: 16
4、 struct.pack_into、 struct.unpack_from 这两个函数在Python手册中有所介绍,但没有给出如何使用的例子。其实它们在实际应用中用的并不多。Google了很久,才找到一个例子,贴出来共享一下:
- #!/usr/bin/env python
- #encoding: utf8
- import sys
- reload(sys)
- sys.setdefaultencoding("utf-8")
- import struct
- from ctypes import create_string_buffer
- buf = create_string_buffer(12)
- print repr(buf.raw) # '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
- struct.pack_into("iii", buf, 0, 1, 2, -1)
- print repr(buf.raw) # '\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff'
- print struct.unpack_from("iii", buf, 0) # (1, 2, -1)
具体内容请参考Python手册 struct 模块
Python手册 struct 模块:http://docs.python.org/library/struct.html#module-struct
struct 类型表
| 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 | 1 | |
| p | char[] | string | ||
| P | void * | integer | (5), (3) |
Notes:
The '?' conversion code corresponds to the _Bool type defined by C99. If this type is not available, it is simulated using a char. In standard mode, it is always represented by one byte.
New in version 2.6.
The 'q' and 'Q' conversion codes are available in native mode only if the platform C compiler supports C long long, or, on Windows, __int64. They are always available in standard modes.
New in version 2.2.
When attempting to pack a non-integer using any of the integer conversion codes, if the non-integer has a __index__() method then that method is called to convert the argument to an integer before packing. If no __index__() method exists, or the call to __index__() raisesTypeError, then the __int__() method is tried. However, the use of __int__() is deprecated, and will raise DeprecationWarning.
Changed in version 2.7: Use of the __index__() method for non-integers is new in 2.7.
Changed in version 2.7: Prior to version 2.7, not all integer conversion codes would use the __int__() method to convert, andDeprecationWarning was raised only for float arguments.
For the 'f' and 'd' conversion codes, the packed representation uses the IEEE 754 binary32 (for 'f') or binary64 (for 'd') format, regardless of the floating-point format used by the platform.
The 'P' format character is only available for the native byte ordering (selected as the default or with the '@' byte order character). The byte order character '=' chooses to use little- or big-endian ordering based on the host system. The struct module does not interpret this as native ordering, so the 'P' format is not available.
A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.
Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.
For the 's' format character, the count is interpreted as the size of the string, not a repeat count like for the other format characters; for example,'10s' means a single 10-byte string, while '10c' means 10 characters. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting string always has exactly the specified number of bytes. As a special case, '0s' means a single, empty string (while '0c' means 0 characters).
The 'p' format character encodes a “Pascal string”, meaning a short variable-length string stored in a fixed number of bytes, given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in to pack() is too long (longer than the count minus 1), only the leading count-1 bytes of the string are stored. If the string is shorter than count-1, it is padded with null bytes so that exactly count bytes in all are used. Note that for unpack(), the 'p' format character consumes count bytes, but that the string returned can never contain more than 255 characters.
For the 'P' format character, the return value is a Python integer or long integer, depending on the size needed to hold a pointer when it has been cast to an integer type. A NULL pointer will always be returned as the Python integer 0. When packing pointer-sized values, Python integer or long integer objects may be used. For example, the Alpha and Merced processors use 64-bit pointer values, meaning a Python long integer will be used to hold the pointer; other platforms use 32-bit pointers and will use a Python integer.
For the '?' format character, the return value is either True or False. When packing, the truth value of the argument object is used. Either 0 or 1 in the native or standard bool representation will be packed, and any non-zero value will be True when unpacking.
字节流与数据类型的相互转换---使用struct模块的更多相关文章
- Python学习——struct模块的pack、unpack示例
he struct module includes functions for converting between strings of bytes and native Python data t ...
- python中struct模块及packet和unpacket
转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...
- 2、粘包现象(struct模块)
昨天我们所做的套接字是有漏洞的,它会出现粘包现象,没有发现这个问题的我们今天会进行演示.今天也会稍微讲解一下基于udp的套接字. 一.基于udp的套接字 udp是无链接的,先启动哪一端都不会报错 ud ...
- 29、粘包现象(struct模块)
昨天我们所做的套接字是有漏洞的,它会出现粘包现象,没有发现这个问题的我们今天会进行演示.今天也会稍微讲解一下基于udp的套接字. 本篇导航: 基于udp的套接字 粘包现象 粘包 解决粘包方法 stru ...
- python中的struct模块
struct模块用于将python中的对象转化为bytes. 举例 demo1:将int转换为bytes buf1 = 256 bin_buf1 = struct.pack('i', buf1) # ...
- python struct模块的使用
struct模块中的函数 函数 return explain pack(fmt,v1,v2…) string 按照给定的格式(fmt),把数据转换成字符串(字节流),并将该字符串返回. pack_in ...
- day30 python学习 struct模块和 subprocess 模块
import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...
- Python:struct模块的pack、unpack
mport struct pack.unpack.pack_into.unpack_from 1 # ref: http://blog.csdn<a href="http://lib. ...
- struct 模块简介
用处 按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时,不能传输int,此时先将int转化为字节流,然后再发送; 按照指定格式将字节流转换为Python指定的数据类型; 处理 ...
随机推荐
- java垃圾回收机制
1 .垃圾回收机制(GC)垃圾回收就是回收内存中不再使用对象:(1)垃圾回收的步骤:1)查找内存中不再使用的对象:2)释放这些对象所占用的内存:(2)查找内存中不再使用的对象方法:1)引用计数法如果一 ...
- iOS 企业证书的使用文档
IN-HOUSE应用程序分发 下面介绍下使用网络方式进行部署的方式,用户直接在iPhone/iPad的Safari浏览器里面输入URL地址即可安装, 注意:目前对于这种企业级开发的应用程序最好的分发方 ...
- 106运用SWITCH语句打印星期几的单词
package com.chongrui.test;/*运用SWITCH语句打印星期几的单词 * */ public class TypeConvertion { public static void ...
- (3)WebApi客户端调用
1.创建一个应用台控制程序,可以把Model的引用,用下面的方法拖拽上来(解决方案里没有这个文件,只是这个文件的引用) 2.Program.cs using System; using System ...
- LightGBM中GBDT的实现
现在LightGBM开源了,这里将之前的一个文档发布出来供大家参考,帮助更快理解LightGBM的实现,整体思路应该是类似的. LightGBM优雅,快速,效果好,希望LightGBM越来越好:) L ...
- ble示例代码
ble代码下载: https://github.com/sutogan4ik/Android-BLE-GATT-Master-Slave
- 【Java EE 学习 55】【酒店会员管理系统项目总结】
本酒店会员管理系统使用了SSH框架和传值播客提供的协同OA静态页面. 项目地址:https://github.com/kdyzm/HotelMembersManagement 一.需求分析 酒店会员管 ...
- POCO库——Foundation组件概述
Foundation组件作为POCO库的基础组件,主要包含了核心Core.缓存Cache.加解密Crypt.日期时间DateTime.动态类型Dynamic.事件events.文件系统Filesyst ...
- C#语言实现定时开启或禁用网卡小程序
C#语言实现定时开启/禁用网卡 程序运行效果图 程序实现主要代码 源代码工程文件(VS2013工程文件编译通过) 查看网卡名称附图 1.win7旗舰版运行效果图: 2.程序实现主要代码: /// 网卡 ...
- PROC 文件系统调节参数介绍(netstat -us)
转自:http://www.cnblogs.com/super-king/p/3296333.html /proc/net/* snmp文件 Ip: ip项 Forwarding : 是 ...