Py学习  »  Python

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

python作业AI毕业设计 • 5 年前 • 383 次点击  

简介

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

  • 功能:二进制和ASCII转换。

  • 类型:标准模块

  • 相关模块:

    1. base64 标准模块。

    2. binhex 标准模块。

    3. uu 标准模块。

    4. quopri 标准模块。

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/uu.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,或QP encoding,没有规范的中文译名,可译为“可打印字符引用编码”、“使用可打印字符的编码”。Quoted-printable是使用可打印的 ASCII字符 (如字母、数字与"=")表示各种编码格式下的字符,以便能在7-bit数据通路上传输8-bit数据, 或者更一般地说在非8-bit clean媒体上正确处理数据。这被定义为MIME content 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("hello world")
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 return value is in the range [-2**31, 2**31-1] regardless of platform. In the past the value 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 return value 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, mrs teal"

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


今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/AaB4IBFz4q
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/26987
 
383 次点击