python模块介绍- binascii 二进制和ASCII转换

python模块介绍-binascii二进制和ASCII转换

 

目录

项目简介... 1

简介:... 2

Uu编码... 2

Binhex编码... 3

Base64编码... 3

QP码... 3

CRC校验和... 4

二进制转换... 4

其他实例... 5

项目简介

Python中文库https://bitbucket.org/xurongzhong/python-chinese-library主要基于个人的使用经验,收集一些重要的外部和内部模块的中文教程和实例。发起人是ouyangchongwu#gmail.comxurongzhong#gmail.com

欢迎大家加入分享经验。联系方法:xurongzhong#gmail.com,微博:http://weibo.com/cizhenshi,python及测试开发qq群1:113938272,群2:6089740。

文件下载:

1,   https://bitbucket.org/xurongzhong/python-chinese-library/downloads。 推荐

2,   hg clone克隆所有文件  hg clone https://bitbucket.org/xurongzhong/python-chinese-library

3,   https://bitbucket.org/xurongzhong/python-chinese-library/src浏览文件,右键点击文件,选另存为下载。

Bug 提交:https://bitbucket.org/xurongzhong/python-chinese-library/issuest

版本管理

版本号

修订发布时间

修订人

备注

V1.0

2013-12-12

Ouyangchongwu#gmail.com

初始版本, 由Effbot库参考和http://docs.python.org/2.7/library/time.html生成。

 

 

 

 

 

 

 

 

 

参考资料:

官方网址: http://docs.python.org/2.7/library/time.html

Effbot库参考:http://effbot.org/librarybook/

简介:

功能:时间访问和转换。

月下载量:

Python版本:Python 1.4以上。

当前版本:

下载地址:

平台:跨平台

               相关模块:

base64标准模块。

binhex标准模块。

uu           标准模块。

quopri 标准模块。

               binascii模块包含很多用来方法来转换二进制和各种ASCII编码的二进制表示法。通常不直接使用这些功能,而是使用封装模块,如uu, base64binhexbinascii模块包含用C语言编写更快的低级功能,通常为高级模块所使用。

Uu编码

uu编码格式现在已经比较少使用(http://zh.wikipedia.org/wiki/Uuencode),相关函数binascii.a2b_uu(string)和binascii.b2a_uu(data)这里不做介绍。

更多资料参见:http://docs.python.org/2/library/uu.html

Binhex编码

Binhex用于Macintosh平台。这里暂不做介绍。相关函数有:binascii.rledecode_hqx(data),binascii.rlecode_hqx(data),binascii.b2a_hqx(data),binascii.crc_hqx(data,crc)。

更多资料参见:http://docs.python.org/2/library/binhex.html

 

 

Base64编码

 

binascii.a2b_base64(string):转换的base64数据块为二进制,并返回二进制数据。一次可以传递多行。和base64.b64decode对应。

binascii.b2a_base64(data):转换二进制数据为一行base64编码的ASCII字符。返回字符串包含换行符。根据base64的标准data的长度最大为57。和base64.b64encode对应。

更多资料参见:http://docs.python.org/2/library/base64.html

 

QP

 

Quoted-printable,或QPencoding,没有规范的中文译名,可译为“可打印字符引用编码”、“使用可打印字符的编码”。Quoted-printable是使用可打印的ASCII字符 (如字母、数字与"=")表示各种编码格式下的字符,以便能在7-bit数据通路上传输8-bit数据, 或者更一般地说在非8-bitclean媒体上正确处理数据。这被定义为MIMEcontent transfer encoding,用于e-mail。

QP使用"="开头的转义字符. 一般限制行宽为76,因为有些软件限制了行宽.

binascii.a2b_qp(string[, header]):转换引述打印数据块为二进制,并返回二进制数据。多行可以在同一时间被传递。如果可选参数头存在和真实,下划线将被解码为空格。

实际上,QP码是是把’\x00’转换成’=00’,也就是替换’\x’为’=’。

>>> s ='\x00='

>>> s = '=\x00hello'

>>> import binascii

>>> encoded =binascii.b2a_qp(s)

>>> encoded

'=3D=00hello'

>>> decoded =binascii.a2b_qp(encoded)

>>> print decoded

=hello

>>> print repr(decoded)

'=\x00hello'

CRC校验和

binascii.crc32(data[, crc]):计算的data 的32位校验和CRC- 32时,crc为初始CRC 。crc32与ZIP文件的校验和一致。

 

>>> print binascii.crc32("helloworld")

222957957

>>> crc =binascii.crc32("hello")

>>> crc =binascii.crc32(" world", crc) & 0xffffffff

>>> print 'crc32 = 0x%08x' %crc

crc32 = 0x0d4a1185

>>> crc

222957957

为了保证跨平台,可以在crc结果上&0xffffffff。原因如下:

Changed in version 2.6: The returnvalue is in the range [-2**31, 2**31-1] regardless of platform. In the past thevalue would be signed on some platforms and unsigned on others. Use &0xffffffff on the value if you want it to match Python 3 behavior.

Changed in version 3.0: The returnvalue is unsigned and in the range [0, 2**32-1] regardless of platform.

二进制转换

binascii.b2a_hex(data)和binascii.hexlify(data):返回二进制数据的十六进制表示。每个字节被转换成相应的2位十六进制表示形式。因此,得到的字符串是是原数据长度的两倍。

binascii.a2b_hex(hexstr)和binascii.unhexlify(hexstr):从十六进制字符串hexstr返回二进制数据。是b2a_hex的逆向操作。 hexstr必须包含偶数个十六进制数字(可以是大写或小写),否则报TypeError。

>>> s = 'hello'

>>> b = b2a_hex(s)

>>> print b

68656c6c6f

>>> a2b_hex(b)

'hello'

>>> b = hexlify(s)

>>> print b

68656c6c6f

>>> unhexlify(b)

'hello'

其他实例

http://effbot.org/librarybook/binascii.htm有如下实例:

import binascii

 

text = "hello, mrsteal"

 

data = binascii.b2a_base64(text)

text = binascii.a2b_base64(data)

print text, "<=>", repr(data)

 

data = binascii.b2a_uu(text)

text = binascii.a2b_uu(data)

print text, "<=>", repr(data)

 

data = binascii.b2a_hqx(text)

text = binascii.a2b_hqx(data)[0]

print text, "<=>", repr(data)

 

# 2.0 and newer

data = binascii.b2a_hex(text)

text = binascii.a2b_hex(data)

print text, "<=>", repr(data)

执行结果:

# python test.py

hello, mrs teal <=> 'aGVsbG8sIG1ycyB0ZWFs\n'

hello, mrs teal <=>'/:&5L;&\\L(&UR<R!T96%L\n'

hello, mrs teal <=> 'D\'9XE\'mX)\'ebFb"dC@&X'

hello, mrs teal <=> '68656c6c6f2c206d7273207465616c'

 

另外单元测试的代码也可供参考:http://svn.python.org/projects/python/branches/tarek_sysconfig/Lib/test/test_binascii.py

 

 

posted on 2013-12-12 17:26  love so much  阅读(3705)  评论(0编辑  收藏  举报

导航