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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM