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


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_* 函數(shù)接受只含有 ASCII 碼的Unicode 字符串。其他函數(shù)只接受 字節(jié)類對象 (例如 bytes,bytearray 和其他支持緩沖區(qū)協(xié)議的對象)。

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

binascii 模塊定義了以下函數(shù):

binascii.a2b_uu(string)?

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

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

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

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

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

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

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)制數(shù)據(jù)轉(zhuǎn)換為一行用 base64 編碼的ASCII字符串。返回值是轉(zhuǎn)換后的行數(shù)據(jù),如果 newline 為true,則返回值包括換行符。該函數(shù)的輸出符合:rfc:3548。

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

binascii.a2b_qp(data, header=False)?

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

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

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

binascii.crc_hqx(data, value)?

value 作為初始 CRC 計算 data 的16位 CRC 值,返回其結(jié)果。這里使用 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)制數(shù)據(jù) data 的十六進(jìn)制表示形式。 data 的每個字節(jié)都被轉(zhuǎn)換為相應(yīng)的2位十六進(jìn)制表示形式。因此返回的字節(jié)對象的長度是 data 的兩倍。

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

如果指定了 sep,它必須為單字符 str 或 bytes 對象。 它將被插入每個 bytes_per_sep 輸入字節(jié)之后。 分隔符位置默認(rèn)從輸出的右端開始計數(shù),如果你希望從左端開始計數(shù),請?zhí)峁┮粋€負(fù)的 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)制數(shù)據(jù)。此函數(shù)功能與 b2a_hex() 相反。 hexstr 必須包含偶數(shù)個十六進(jìn)制數(shù)字(可以是大寫或小寫),否則會引發(fā) Error 異常。

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

exception binascii.Error?

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

exception binascii.Incomplete?

數(shù)據(jù)不完整引發(fā)的異常。通常不是編程錯誤導(dǎo)致的,可以通過讀取更多的數(shù)據(jù)并再次嘗試來處理該異常。

參見

模塊 base64

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

模塊 uu

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

模塊 quopri

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