This document is for an old version of Python that is no longer supported. You should upgrade and read the Python 当前稳定版本的文档.

# 18.14. `binascii` — 二进制和 ASCII 码互转

`binascii` 模块包含很多在二进制和二进制表示的各种ASCII码之间转换的方法。 通常情况不会直接使用这些函数，而是使用像 `uu` ， `base64` ，或 `binhex` 这样的封装模块。 为了执行效率高，`binascii` 模块含有许多用 C 写的低级函数，这些底层函数被一些高级模块所使用。

`binascii` 模块定义了以下函数：

`binascii.``a2b_uu`(string)

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

`binascii.``b2a_uu`(data)

  Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char. The length of data should be at most 45.

`binascii.``a2b_base64`(string)

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

`binascii.``b2a_base64`(data)

  Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char. The newline is added because the original use case for this function was to feed it a series of 57 byte input lines to get output lines that conform to the MIME-base64 standard. Otherwise the output conforms to RFC 3548.

`binascii.``a2b_qp`(string[, header])

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

`binascii.``b2a_qp`(data[, quotetabs, istext, header])

  Convert binary data to a line(s) of ASCII characters in quoted-printable encoding. The return value is the converted line(s). If the optional argument quotetabs is present and true, all tabs and spaces will be encoded. If the optional argument istext is present and true, newlines are not encoded but trailing whitespace will be encoded. If the optional argument header is present and true, spaces will be encoded as underscores per RFC1522. If the optional argument header is present and false, newline characters will be encoded as well; otherwise linefeed conversion might corrupt the binary data stream.

`binascii.``a2b_hqx`(string)

  将 binhex4 格式的 ASCII 数据不进行 RLE 解压缩直接转换为二进制数据。该字符串应包含完整数量的二进制字节，或者（在binhex4 数据最后部分）剩余位为零。

`binascii.``rledecode_hqx`(data)

  根据 binhex4 标准对数据执行 RLE 解压缩。该算法在一个字节的数据后使用 `0x90` 作为重复指示符，然后计数。计数 `0` 指定字节值 `0x90` 。该例程返回解压缩的数据，输入数据以孤立的重复指示符结束的情况下，将引发 `Incomplete` 异常。

`binascii.``rlecode_hqx`(data)

  在 data 上执行 binhex4 游程编码压缩并返回结果。

`binascii.``b2a_hqx`(data)

  执行 hexbin4 类型二进制到 ASCII 码的转换并返回结果字符串。输入数据应经过 RLE 编码，且数据长度可被3整除（除了最后一个片段）。

`binascii.``crc_hqx`(data, crc)

  Compute a 16-bit CRC value of data, starting with an initial crc and returning the result. This uses the CRC-CCITT polynomial x16 + x12 + x5 + 1, often represented as 0x1021. This CRC is used in the binhex4 format.

`binascii.``crc32`(data[, crc])

  Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This 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("hello world")
      # Or, in two pieces:
      crc = binascii.crc32("hello")
      crc = binascii.crc32(" world", crc) & 0xffffffff
      print 'crc32 = 0x%08x' % crc

注解

To generate the same numeric value across all Python versions and platforms use crc32(data) & 0xffffffff. If you are only using the checksum in packed binary format this is not necessary as the return value is the correct 32bit binary representation regardless of sign.

在 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.

在 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)

  Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.

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

  Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of `b2a_hex()`. hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise a `TypeError` is raised.

exception `binascii.``Error`

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

exception `binascii.``Incomplete`

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

参见

模块 `base64`

  Support for RFC compliant base64-style encoding in base 16, 32, and 64.

模块 `binhex`

  支持在 Macintosh 上使用的 binhex 格式。

模块 `uu`

  支持在 Unix 上使用的 UU 编码。

模块 `quopri`

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