binasciiConvert between binary and ASCII在二进制和ASCII之间转换


The binascii module contains a number of methods to convert between binary and various ASCII-encoded binary representations. binascii模块包含许多在二进制和各种ASCII编码二进制表示之间转换的方法。Normally, you will not use these functions directly but use wrapper modules like uu, base64, or binhex instead. 通常,您不会直接使用这些函数,而是使用诸如uubase64binhex之类的包装器模块。The binascii module contains low-level functions written in C for greater speed that are used by the higher-level modules.binascii模块包含用C编写的低级函数,以提高高级模块使用的速度。

Note

a2b_* functions accept Unicode strings containing only ASCII characters. a2b_*函数接受仅包含ASCII字符的Unicode字符串。Other functions only accept bytes-like objects (such as bytes, bytearray and other objects that support the buffer protocol).其他函数只接受类似字节的对象(例如bytesbytearray和其他支持缓冲区协议的对象)。

Changed in version 3.3:版本3.3中更改: ASCII-only unicode strings are now accepted by the a2b_* functions.a2b_*函数现在只接受ASCII unicode字符串。

The binascii module defines the following functions:binascii模块定义了以下功能:

binascii.a2b_uu(string)

Convert a single line of uuencoded data back to binary and return the binary data. 将一行uuencoded数据转换回二进制并返回二进制数据。Lines normally contain 45 (binary) bytes, except for the last line. 除最后一行外,行通常包含45(二进制)字节。Line data may be followed by whitespace.行数据后面可以是空白。

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

Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char. 将二进制数据转换为一行ASCII字符,返回值是转换后的行,包括换行符。The length of data should be at most 45. data长度最多应为45。If backtick is true, zeros are represented by '`' instead of spaces.如果backticktrue,则用'`'而不是空格表示零。

Changed in version 3.7:版本3.7中更改: Added the backtick parameter.添加了backtick参数。

binascii.a2b_base64(string)

Convert a block of base64 data back to binary and return the binary data. 将base64数据块转换回二进制并返回二进制数据。More than one line may be passed at a time.一次可以通过多行。

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

Convert binary data to a line of ASCII characters in base64 coding. 以base64编码将二进制数据转换为一行ASCII字符。The return value is the converted line, including a newline char if newline is true. 返回值是转换后的行,如果newlinetrue,则包含换行符。The output of this function conforms to RFC 3548.此函数的输出符合RFC 3548

Changed in version 3.6:版本3.6中更改: Added the newline parameter.添加了newline参数。

binascii.a2b_qp(data, header=False)

Convert a block of quoted-printable data back to binary and return the binary data. 将带引号的可打印数据块转换回二进制并返回二进制数据。More than one line may be passed at a time. 一次可以通过多行。If the optional argument header is present and true, underscores will be decoded as spaces.如果可选参数header存在且为true,则下划线将被解码为空格。

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

Convert binary data to a line(s) of ASCII characters in quoted-printable encoding. 将二进制数据转换为带引号的可打印编码的ASCII字符行。The return value is the converted line(s). 返回值是转换后的行。If the optional argument quotetabs is present and true, all tabs and spaces will be encoded. 如果可选参数quotetabs存在且为真,则将对所有制表符和空格进行编码。If the optional argument istext is present and true, newlines are not encoded but trailing whitespace will be encoded. 如果可选参数istext存在且为true,则不会对换行符进行编码,但会对尾随空格进行编码。If the optional argument header is present and true, spaces will be encoded as underscores per RFC 1522. 如果可选参数标头存在且为真,则空格将按照RFC 1522编码为下划线。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.如果可选参数头存在且为false,则换行符也将被编码;否则换行转换可能会损坏二进制数据流。

binascii.a2b_hqx(string)

Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression. 将binhex4格式的ASCII数据转换为二进制数据,无需进行RLE解压缩。The string should contain a complete number of binary bytes, or (in case of the last portion of the binhex4 data) have the remaining bits zero.字符串应包含完整的二进制字节数,或者(如果是binhex4数据的最后一部分)剩余的位为零。

Deprecated since version 3.9.自3.9版起已弃用。

binascii.rledecode_hqx(data)

Perform RLE-decompression on the data, as per the binhex4 standard. 根据binhex4标准对数据执行RLE解压缩。The algorithm uses 0x90 after a byte as a repeat indicator, followed by a count. 该算法使用字节后的0x90作为重复指示符,后跟计数。A count of 0 specifies a byte value of 0x90. 计数0指定字节值0x90The routine returns the decompressed data, unless data input data ends in an orphaned repeat indicator, in which case the Incomplete exception is raised.例程返回解压缩的数据,除非数据输入数据以孤立重复指示符结尾,在这种情况下会引发Incomplete异常。

Changed in version 3.2:版本3.2中更改: Accept only bytestring or bytearray objects as input.仅接受字节字符串或字节数组对象作为输入。

Deprecated since version 3.9.自3.9版起已弃用。

binascii.rlecode_hqx(data)

Perform binhex4 style RLE-compression on data and return the result.data执行binhex4样式的RLE压缩并返回结果。

Deprecated since version 3.9.自3.9版起已弃用。

binascii.b2a_hqx(data)

Perform hexbin4 binary-to-ASCII translation and return the resulting string. 执行hexbin4二进制到ASCII的转换并返回结果字符串。The argument should already be RLE-coded, and have a length divisible by 3 (except possibly the last fragment).参数应该已经是RLE编码的,并且长度可以被3整除(可能最后一个片段除外)。

Deprecated since version 3.9.自3.9版起已弃用。

binascii.crc_hqx(data, value)

Compute a 16-bit CRC value of data, starting with value as the initial CRC, and return the result. 计算data的16位CRC值,以value作为初始CRC,并返回结果。This uses the CRC-CCITT polynomial x16 + x12 + x5 + 1, often represented as 0x1021. 这使用CRC-CCITT多项式x16+x12+x5+1,通常表示为0x1021This CRC is used in the binhex4 format.该CRC以二进制4格式使用。

binascii.crc32(data[, value])

Compute CRC-32, the unsigned 32-bit checksum of data, starting with an initial CRC of value. 计算CRC-32,data的无符号32位校验和,从value的初始CRC开始。The default initial CRC is zero. 默认初始CRC为零。The algorithm is consistent with the ZIP file checksum. 该算法与ZIP文件校验和一致。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))

Changed in version 3.0:版本3.0中更改: The result is always unsigned. 结果总是无符号的。To generate the same numeric value when using Python 2 or earlier, use crc32(data) & 0xffffffff.要在使用Python 2或更早版本时生成相同的数值,请使用crc32(data) & 0xffffffff

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

Return the hexadecimal representation of the binary data. 返回二进制data的十六进制表示。Every byte of data is converted into the corresponding 2-digit hex representation. data的每个字节都被转换为相应的2位十六进制表示。The returned bytes object is therefore twice as long as the length of data.因此,返回的字节对象是data长度的两倍。

Similar functionality (but returning a text string) is also conveniently accessible using the bytes.hex() method.使用bytes.hex()方法也可以方便地访问类似的功能(但返回文本字符串)。

If sep is specified, it must be a single character str or bytes object. 如果指定了sep,它必须是一个单字符str或字节对象。It will be inserted in the output after every bytes_per_sep input bytes. 它将在每个bytes_per_sep输入字节之后插入到输出中。Separator placement is counted from the right end of the output by default, if you wish to count from the left, supply a negative bytes_per_sep value.默认情况下,分隔符放置从输出的右端开始计数,如果希望从左侧开始计数,请提供一个负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'

Changed in version 3.8:版本3.8中更改: The sep and bytes_per_sep parameters were added.添加了sepbytes_per_sep参数。

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

Return the binary data represented by the hexadecimal string hexstr. 返回十六进制字符串hexstr表示的二进制数据。This function is the inverse of b2a_hex(). 此函数是b2a_hex()的逆函数。hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise an Error exception is raised.hexstr必须包含偶数个十六进制数字(可以是大写或小写),否则会引发Error异常。

Similar functionality (accepting only text string arguments, but more liberal towards whitespace) is also accessible using the bytes.fromhex() class method.使用bytes.fromhex()类方法也可以访问类似的功能(只接受文本字符串参数,但对空格更为宽松)。

exceptionbinascii.Error

Exception raised on errors. 出现错误时引发异常。These are usually programming errors.这些通常是编程错误。

exceptionbinascii.Incomplete

Exception raised on incomplete data. These are usually not programming errors, but may be handled by reading a little more data and trying again.数据不完整时引发异常。这些通常不是编程错误,但可以通过读取更多数据并重试来处理。

See also

Module base64

Support for RFC compliant base64-style encoding in base 16, 32, 64, and 85.支持16、32、64和85进制中符合RFC的base64样式编码。

Module binhex

Support for the binhex format used on the Macintosh.支持Macintosh上使用的binhex格式。

Module uu

Support for UU encoding used on Unix.支持Unix上使用的UU编码。

Module quopri

Support for quoted-printable encoding used in MIME email messages.支持MIME电子邮件中使用的引用可打印编码。