Convert between binary and ASCII using Python (binascii)


The binascii module enables conversion between binary and various ASCII encoded binary representations. The binascii module contains low-level functions written in C for greater speed. They are used by the higher-level modules such as uu, base64 or binhex modules.

The binascii module defines the following functions. These function are named as a2b_* or b2a_*

binascii.a2b_uu(string): Convert a single line of uuencoded data back to binary and return the binary data. Lines normally contain 45 (binary) bytes, except for the last line. Line data may be followed by white space.

binascii.b2a_uu(data): Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char.

binascii.a2b_base64(string): Convert a block of base64 data back to binary and return the binary data.

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 if a newline is true.

binascii.a2b_qp(data): Convert a block of quoted-printable data back to binary and return the binary data.

binascii.b2a_qp(data): Convert binary data to a line(s) of ASCII characters in quoted-printable encoding. The return value is the converted line(s).

binascii.a2b_hqx(string): Convert binhex4 formatted ASCII data to binary, without doing RLEdecompression. The string should contain a complete number of binary bytes, or have the remaining bits zero.

binascii.rledecode_hqx(data): Perform REL-decompression on the data, as per the binhex4 standard.

binascii.rlecode_hqx(data): Perform binhex4 style RLE-compression on data and return the result.

binascii.b2a_hqx(data): Perform hexbin4 binary-to-ASCII translation and return the resulting string. The argument should already be RLE-coded.

binascii.crc_hqx(data, value): Compute a 16-bit CRC value of data, starting with value as the initial CRC, and return the result.

binascii.crc32(data[, value]): Compute CRC-32, the 32-bit checksum of data, starting with an initial CRC of value. The default initial CRC is zero.

Updated on: 30-Jul-2019

694 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements