binascii --- 二進(jìn)制和 ASCII 碼互轉?


The binascii module contains a number of methods to convert between binary and various ASCII-encoded binary representations. Normally, you will not use these functions directly but use wrapper modules like uu or base64 instead. The binascii module contains low-level functions written in C for greater speed that are used by the higher-level modules.

備注

a2b_* 函數接受只含有 ASCII 碼的Unicode 字符串。其他函數只接受 字節類(lèi)對象 (例如 bytes,bytearray 和其他支持緩沖區協(xié)議的對象)。

在 3.3 版更改: ASCII-only unicode strings are now accepted by the a2b_* functions.

binascii 模塊定義了以下函數:

binascii.a2b_uu(string)?

將單行 uu 編碼數據轉換成二進(jìn)制數據并返回。uu 編碼每行的數據通常包含45 個(gè)(二進(jìn)制)字節,最后一行除外。每行數據后面可能跟有空格。

binascii.b2a_uu(data, *, backtick=False)?

將二進(jìn)制數據轉換為 ASCII 編碼字符,返回值是轉換后的行數據,包括換行符。 data 的長(cháng)度最多為45。如果 backtick 為ture,則零由 '`' 而不是空格表示。

在 3.7 版更改: 增加 backtick 形參。

binascii.a2b_base64(string, /, *, strict_mode=False)?

將 base64 數據塊轉換成二進(jìn)制并以二進(jìn)制數據形式返回。一次可以傳遞多行數據。

If strict_mode is true, only valid base64 data will be converted. Invalid base64 data will raise binascii.Error.

Valid base64:
  • Conforms to RFC 3548.

  • Contains only characters from the base64 alphabet.

  • Contains no excess data after padding (including excess padding, newlines, etc.).

  • Does not start with a padding.

在 3.11 版更改: Added the strict_mode parameter.

binascii.b2a_base64(data, *, newline=True)?

將二進(jìn)制數據轉換為一行用 base64 編碼的ASCII字符串。返回值是轉換后的行數據,如果 newline 為true,則返回值包括換行符。該函數的輸出符合:rfc:3548。

在 3.6 版更改: 增加 newline 形參。

binascii.a2b_qp(data, header=False)?

將一個(gè)引號可打印的數據塊轉換成二進(jìn)制數據并返回。一次可以轉換多行。如果可選參數 header 存在且為true,則數據中的下劃線(xiàn)將被解碼成空格。

binascii.b2a_qp(data, quotetabs=False, istext=True, header=False)?

將二進(jìn)制數據轉換為一行或多行帶引號可打印編碼的ASCII字符串。返回值是轉換后的行數據。如果可選參數 quotetabs 存在且為真值,則對所有制表符和空格進(jìn)行編碼。如果可選參數 istext 存在且為真值,則不對新行進(jìn)行編碼,但將對尾隨空格進(jìn)行編碼。如果可選參數 header 存在且為true,則空格將被編碼為下劃線(xiàn) RFC 1522。如果可選參數 header 存在且為假值,則也會(huì )對換行符進(jìn)行編碼;不進(jìn)行換行轉換編碼可能會(huì )破壞二進(jìn)制數據流。

binascii.crc_hqx(data, value)?

value 作為初始 CRC 計算 data 的16位 CRC 值,返回其結果。這里使用 CRC-CCITT 生成多項式 x16 + x12 + x5 + 1 ,通常表示為0x1021。該 CRC 被用于 binhex4 格式。

binascii.crc32(data[, value])?

Compute CRC-32, the unsigned 32-bit checksum of data, starting with an initial CRC of value. The default initial CRC is zero. The algorithm is consistent with the ZIP file checksum. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. Use as follows:

print(binascii.crc32(b"hello world"))
# Or, in two pieces:
crc = binascii.crc32(b"hello")
crc = binascii.crc32(b" world", crc)
print('crc32 = {:#010x}'.format(crc))

在 3.0 版更改: The result is always unsigned.

binascii.b2a_hex(data[, sep[, bytes_per_sep=1]])?
binascii.hexlify(data[, sep[, bytes_per_sep=1]])?

返回二進(jìn)制數據 data 的十六進(jìn)制表示形式。 data 的每個(gè)字節都被轉換為相應的2位十六進(jìn)制表示形式。因此返回的字節對象的長(cháng)度是 data 的兩倍。

使用:bytes.hex() 方法也可以方便地實(shí)現相似的功能(但僅返回文本字符串)。

如果指定了 sep,它必須為單字符 str 或 bytes 對象。 它將被插入每個(gè) bytes_per_sep 輸入字節之后。 分隔符位置默認從輸出的右端開(kāi)始計數,如果你希望從左端開(kāi)始計數,請提供一個(gè)負的 bytes_per_sep 值。

>>>
>>> import binascii
>>> binascii.b2a_hex(b'\xb9\x01\xef')
b'b901ef'
>>> binascii.hexlify(b'\xb9\x01\xef', '-')
b'b9-01-ef'
>>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2)
b'b9_01ef'
>>> binascii.b2a_hex(b'\xb9\x01\xef', b' ', -2)
b'b901 ef'

在 3.8 版更改: 添加了 sepbytes_per_sep 形參。

binascii.a2b_hex(hexstr)?
binascii.unhexlify(hexstr)?

返回由十六進(jìn)制字符串 hexstr 表示的二進(jìn)制數據。此函數功能與 b2a_hex() 相反。 hexstr 必須包含偶數個(gè)十六進(jìn)制數字(可以是大寫(xiě)或小寫(xiě)),否則會(huì )引發(fā) Error 異常。

使用:bytes.fromhex() 類(lèi)方法也實(shí)現相似的功能(僅接受文本字符串參數,不限制其中的空白字符)。

exception binascii.Error?

通常是因為編程錯誤引發(fā)的異常。

exception binascii.Incomplete?

數據不完整引發(fā)的異常。通常不是編程錯誤導致的,可以通過(guò)讀取更多的數據并再次嘗試來(lái)處理該異常。

參見(jiàn)

模塊 base64

支持在16,32,64,85進(jìn)制中進(jìn)行符合 RFC 協(xié)議的 base64 樣式編碼。

模塊 uu

支持在 Unix 上使用的 UU 編碼。

模塊 quopri

支持在 MIME 版本電子郵件中使用引號可打印編碼。