# `binascii` --- 在二进制数据和 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 `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 字符串。其他函数只接受 字节类对象 （例如 `bytes`，`bytearray` 和其他支持缓冲区协议的对象）。

在 3.3 版本发生变更: ASCII-only unicode strings are now accepted by the `a2b_*` functions.

The `binascii` module defines the following functions:

binascii.a2b_uu(string)

  将单行 uu 编码数据转换成二进制数据并返回。uu 编码每行的数据通常包含45 个（二进制）字节，最后一行除外。每行数据后面可能跟有空格。

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

  将二进制数据转换为 ASCII 编码字符，返回值是转换后的行数据，包括换行符。 data 的长度最多为45。如果 backtick 为ture，则零由 `'`'` 而不是空格表示。

  在 3.7 版本发生变更: 增加 backtick 形参。

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

  将 base64 数据块转换成二进制并以二进制数据形式返回。一次可以传递多行数据。

  If ignorechars is specified, it should be a bytes-like object containing characters to ignore from the input when strict_mode is true. If ignorechars contains the pad character `'='`, the pad characters presented before the end of the encoded data and the excess pad characters will be ignored. The default value of strict_mode is `True` if ignorechars is specified, `False` otherwise.

  如果 strict_mode 为真值，则将只转换有效的 base64 数据。 无效的 base64 数据将会引发 `binascii.Error`。

  有效的 base64:

  - Conforms to RFC 4648.

  - 仅包含来自 base64 字符表的字符。

  - 不包含填充后的额外数据（包括冗余填充、换行符等）。

  - 不以填充符打头。

  在 3.11 版本发生变更: 增加了 strict_mode 形参。

  在 3.15.0a5 (unreleased) 版本发生变更: Added the ignorechars parameter.

binascii.b2a_base64(data, *, wrapcol=0, newline=True)

  Convert binary data to a line(s) of ASCII characters in base64 coding, as specified in RFC 4648.

  If wrapcol is non-zero, insert a newline (`b'\n'`) character after at most every wrapcol characters. If wrapcol is zero (default), do not insert any newlines.

  If newline is true (default), a newline character will be added at the end of the output.

  在 3.6 版本发生变更: 增加 newline 形参。

  在 3.15 版本发生变更: Added the wrapcol parameter.

binascii.a2b_ascii85(string, /, *, foldspaces=False, adobe=False, ignorechars=b'')

  Convert Ascii85 data back to binary and return the binary data.

  Valid Ascii85 data contains characters from the Ascii85 alphabet in groups of five (except for the final group, which may have from two to five characters). Each group encodes 32 bits of binary data in the range from `0` to `2 ** 32 - 1`, inclusive. The special character `z` is accepted as a short form of the group `!!!!!`, which encodes four consecutive null bytes.

  foldspaces is a flag that specifies whether the 'y' short sequence should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is not supported by the "standard" Ascii85 encoding.

  adobe controls whether the input sequence is in Adobe Ascii85 format (i.e. is framed with <~ and ~>).

  ignorechars should be a bytes-like object containing characters to ignore from the input. This should only contain whitespace characters.

  Invalid Ascii85 data will raise `binascii.Error`.

  Added in version 3.15.0a5 (unreleased).

binascii.b2a_ascii85(data, /, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)

  Convert binary data to a formatted sequence of ASCII characters in Ascii85 coding. The return value is the converted data.

  foldspaces is an optional flag that uses the special short sequence 'y' instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This feature is not supported by the "standard" Ascii85 encoding.

  If wrapcol is non-zero, insert a newline (`b'\n'`) character after at most every wrapcol characters. If wrapcol is zero (default), do not insert any newlines.

  If pad is true, the input is padded with `b'\0'` so its length is a multiple of 4 bytes before encoding. Note that the `btoa` implementation always pads.

  adobe controls whether the encoded byte sequence is framed with `<~` and `~>`, which is used by the Adobe implementation.

  Added in version 3.15.0a5 (unreleased).

binascii.a2b_base85(string, /)

  Convert Base85 data back to binary and return the binary data. More than one line may be passed at a time.

  Valid Base85 data contains characters from the Base85 alphabet in groups of five (except for the final group, which may have from two to five characters). Each group encodes 32 bits of binary data in the range from `0` to `2 ** 32 - 1`, inclusive.

  Invalid Base85 data will raise `binascii.Error`.

  Added in version 3.15.0a5 (unreleased).

binascii.b2a_base85(data, /, *, pad=False)

  Convert binary data to a line of ASCII characters in Base85 coding. The return value is the converted line.

  If pad is true, the input is padded with `b'\0'` so its length is a multiple of 4 bytes before encoding.

  Added in version 3.15.0a5 (unreleased).

binascii.a2b_z85(string, /)

  Convert Z85 data back to binary and return the binary data. More than one line may be passed at a time.

  Valid Z85 data contains characters from the Z85 alphabet in groups of five (except for the final group, which may have from two to five characters). Each group encodes 32 bits of binary data in the range from `0` to `2 ** 32 - 1`, inclusive.

  See Z85 specification for more information.

  Invalid Z85 data will raise `binascii.Error`.

  Added in version 3.15.0a5 (unreleased).

binascii.b2a_z85(data, /, *, pad=False)

  Convert binary data to a line of ASCII characters in Z85 coding. The return value is the converted line.

  If pad is true, the input is padded with `b'\0'` so its length is a multiple of 4 bytes before encoding.

  See Z85 specification for more information.

  Added in version 3.15.0a5 (unreleased).

binascii.a2b_qp(data, header=False)

  将一个引号可打印的数据块转换成二进制数据并返回。一次可以转换多行。如果可选参数 header 存在且为true，则数据中的下划线将被解码成空格。

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

  将二进制数据转换为一行或多行带引号可打印编码的ASCII字符串。返回值是转换后的行数据。如果可选参数 quotetabs 存在且为真值，则对所有制表符和空格进行编码。如果可选参数 istext 存在且为真值，则不对新行进行编码，但将对尾随空格进行编码。如果可选参数 header 存在且为true，则空格将被编码为下划线 RFC 1522。如果可选参数 header 存在且为假值，则也会对换行符进行编码;不进行换行转换编码可能会破坏二进制数据流。

binascii.crc_hqx(data, value)

  以 value 作为初始 CRC 计算 data 的16位 CRC 值，返回其结果。这里使用 CRC-CCITT 生成多项式 x16 + x12 + x5 + 1 ，通常表示为0x1021。该 CRC 被用于 binhex4 格式。

binascii.crc32(data[, value])

  计算 CRC-32，即 data 的无符号 32 位校验和，初始 CRC 值为 value。 默认的初始 CRC 值为零。 该算法与 ZIP 文件校验和算法一致。 由于该算法被设计用作校验和算法，因此不适合用作通用哈希算法。 使用方式如下:

      print(binascii.crc32(b"hello world"))
      # 或者，分成两块：
      crc = binascii.crc32(b"hello")
      crc = binascii.crc32(b" world", crc)
      print('crc32 = {:#010x}'.format(crc))

  在 3.0 版本发生变更: 结果将总是不带符号的。

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

  返回二进制数据 data 的十六进制表示形式。 data 的每个字节都被转换为相应的2位十六进制表示形式。因此返回的字节对象的长度是 data 的两倍。

  使用：`bytes.hex()` 方法也可以方便地实现相似的功能（但仅返回文本字符串）。

  如果指定了 sep，它必须为单字符 str 或 bytes 对象。 它将被插入每个 bytes_per_sep 输入字节之后。 分隔符位置默认从输出的右端开始计数，如果你希望从左端开始计数，请提供一个负的 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 版本发生变更: 添加了 sep 和 bytes_per_sep 形参。

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

  返回由十六进制字符串 hexstr 表示的二进制数据。此函数功能与 `b2a_hex()` 相反。 hexstr 必须包含偶数个十六进制数字（可以是大写或小写），否则会引发 `Error` 异常。

  使用：`bytes.fromhex()` 类方法也实现相似的功能（仅接受文本字符串参数，不限制其中的空白字符）。

exception binascii.Error

  通常是因为编程错误引发的异常。

exception binascii.Incomplete

  数据不完整引发的异常。通常不是编程错误导致的，可以通过读取更多的数据并再次尝试来处理该异常。

参见

模块 `base64`

  支持在16，32，64，85进制中进行符合 RFC 协议的 base64 样式编码。

模块 `quopri`

  支持在 MIME 版本电子邮件中使用引号可打印编码。