[雪峯磁針石博客]python標準模塊介紹- binascii 二進制和ASCII轉換

簡介

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章