python標準模組介紹- binascii 二進位制和ASCII轉換
簡介
binascii模組包含很多用來方法來轉換二進位制和各種ASCII編碼的二進位制表示法。通常不直接使用這些功能,而是使用封裝模組,如uu, base64或binhex。binascii模組包含用C語言編寫更快的低階功能,通常為高階模組所使用。
-
功能:二進位制和ASCII轉換。
-
型別:標準模組
-
相關模組:
-
base64 標準模組。
-
binhex 標準模組。
-
uu標準模組。
-
quopri標準模組。
-
Uu編碼
uu編碼格式現在已經比較少使用(ofollow,noindex">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'
參考資料
- python測試等IT技術支援qq群: 144081101(後期會錄製視訊存在該群群檔案) 591302926 567351477 釘釘免費群:21745728
- 本文最新版本地址
- 本文涉及的python測試開發庫 謝謝點贊!
- 本文相關海量書籍下載
- python官方文件:https://docs.python.org/3/library/binascii.html
- http://effbot.org/librarybook/binascii.htm
其他例項
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