Brief introduction
The Binascii module contains a number of binary representations that are used to convert binary and various ASCII encodings. These features are not typically used directly, but instead are encapsulated modules such as UU, Base64, or BinHex. The Binascii module contains faster, lower-level features written in C that are typically used by advanced modules.
UU code
The UU encoding format is now less used (http://zh.wikipedia.org/wiki/Uuencode), related functions Binascii.a2b_uu (string) and Binascii.b2a_uu ( Data) is not introduced here. For more information see: http://docs.python.org/2/library/uu.html
BinHex encoding
BinHex for the Macintosh platform. There is no introduction here. The relevant functions are: BINASCII.RLEDECODE_HQX (data), BINASCII.RLECODE_HQX (data), BINASCII.B2A_HQX (data), BINASCII.CRC_HQX (data, CRC )。 For more information see : http://docs.python.org/2/library/uu.html
BASE64 encoding
Binascii.a2b_base64 (String): The converted Base64 data block is binary and returns binary data. You can pass more than one line at a time. and base64. B64decode correspondence. Binascii.b2a_base64 (data): Converts the binary data to one line of Base64 encoded ASCII characters. The return string contains a line break. The maximum length of the standard data according to Base64 is 57. and base64. B64encode correspondence. For more information see: http://docs.python.org/2/library/base64.html
QP Code
Quoted-printable, or QP encoding, there is no standard Chinese translation, can be translated as "printable character reference Code", "Use printable character encoding." Quoted-printable is the use of printable ASCII characters (such as letters, numbers, and "=") to denote characters in various encoding formats, so that 8-bit data can be transmitted on the 7-bit data path, or that the data is processed correctly on non-8-bit clean media. This is defined as MIME content transfer encoding, which is used for e-mail.
QP uses an escape character that begins with "=". The general limit for line width is 76, because some software restricts the line width.
BINASCII.A2B_QP (string[, header]): Convert the quoted print data block to binary, and return binary data. Multiple lines can be passed at the same time. If the optional parameter header is present and true, the underscore is decoded to a space.
In fact, the QP code is to convert ' \x00 ' to ' = 00 ', which is to replace ' \x ' as ' = '.
>>> s = ' \x00= ' >>> s = ' =\x00hello ' >>> import binascii>>> encoded = Binascii.b2a_ QP (s) >>> encoded ' =3d=00hello ' >>> decoded = BINASCII.A2B_QP (encoded) >>> print decoded= hello>>> print repr (decoded) ' =\x00hello '
CRC Checksum
BINASCII.CRC32 (data[, CRC]): The CRC is the initial CRC when calculating the 32-bit checksum CRC-32 of data. CRC32 is consistent with the checksum of the zip file.
>>> print binascii.crc32 ("Hello World") 222957957>>> CRC = Binascii.crc32 ("Hello") >>> CRC = BINASCII.CRC32 ("World", CRC) & 0xffffffff>>> print ' CRC32 = 0x%08x '% CRCCRC32 = 0x0d4a1185>>> Crc2 22957957
To ensure cross-platform, the CRC results can be & 0xFFFFFFFF. The reasons are as follows:
Changed in version 2.6:the return value was in the range [ -2**31, 2**31-1] regardless of platform. In the past the value would is signed on some platforms and unsigned on others. Use & 0xFFFFFFFF in the value if you want it to match Python 3 behavior. Changed in version 3.0:the return value was unsigned and in the range [0, 2**32-1] regardless of platform.
Binary conversions
Binascii.b2a_hex (data) and binascii.hexlify (data): Returns the hexadecimal representation of the binary data. Each byte is converted into a corresponding 2-bit hexadecimal representation. Therefore, the resulting string is twice times the length of the original data. Binascii.a2b_hex (HEXSTR) and Binascii.unhexlify (HEXSTR): Returns binary data from the hexadecimal string hexstr. Is the reverse operation of the B2a_hex. The hexstr must contain an even number of hexadecimal digits (either uppercase or lowercase), otherwise the TypeError is reported.
>>> s = ' Hello ' >>> b = B2a_hex (s) >>> print b68656c6c6f>>> A2b_hex (b) ' Hello ' >> > b = hexlify (s) >>> print b68656c6c6f>>> unhexlify (b) ' Hello '
Other instances
Http://effbot.org/librarybook/binascii.htm has the following examples:
Import binasciitext = "Hello, Mrs Teal" data = binascii.b2a_base64 (text) Text = binascii.a2b_base64 (data) print text, "<= > ", repr (data) data = Binascii.b2a_uu (text) Text = binascii.a2b_uu (data) print text," <=> ", repr (data) data = BINASCII.B2A_HQX (text) Text = binascii.a2b_hqx (data) [0]print text, "<=>", repr (data) # 2.0 and Newerdata = Binascii.b2a_hex (text) Text = Binascii.a2b_hex (data) print text, "<=>", repr (data)
Execution Result:
# python test.py Hello, Mrs Teal <=> ' agvsbg8sig1ycyb0zwfs\n ' Hello, Mrs Teal <=> '/:&5l;&\\l (&ur& Lt R! T96%l\n ' Hello, Mrs Teal <=> ' d\ ' 9xe\ ' mX] \ ' EBFB ' [email protected]&x ' Hello, Mrs Teal <=> ' 68656C6C6F2C206D7273207465616C '
In addition, the Code for unit tests is alsoavailable for reference: http://svn.python.org/projects/python/branches/tarek_sysconfig/Lib/test/test_binascii.py
Resources
Http://docs.python.org/2/library/binascii.html
Http://effbot.org/librarybook/binascii.htm
Author Blog: http://my.oschina.net/u/1433482
Genre: Translation plus finishing
Python Module Introduction-binascii binary and ASCII conversion