Python binary to ascii

The binascii module consists of a number of methods to transform betweenbinary and also assorted ASCII-encoded binary depictions. Generally, you will certainly notuse these functions straight however usage wrapper modules like uu,base64, or binhex instead. The binascii module containslow-level features created in C for better speed that are provided by thehigher-level modules.

You watching: Python binary to ascii


a2b_* attributes accept Unicode strings containing only ASCII personalities.Other functions just accept bytes-favor objects (such asbytes, bytevariety and also various other objects that assistance the bufferprotocol).

The binascii module specifies the following functions:


Convert a solitary line of uuencoded information back to binary and also return the binaryinformation. Lines generally contain 45 (binary) bytes, except for the last line. Linedata might be adhered to by whiteroom.

binascii.b2a_uu(data, *, backtick=False

Convert binary information to a line of ASCII personalities, the return worth is theconverted line, including a newline char. The length of data have to be at most45. If backtick is true, zeros are stood for by "`" instead of spaces.


Convert a block of base64 data back to binary and also rerotate the binary information. Morethan one line might be passed at a time.

binascii.b2a_base64(data, *, newline=True

Convert binary data to a line of ASCII personalities in base64 coding. The returnworth is the converted line, consisting of a newline char if newline istrue. The output of this feature condevelops to RFC 3548.

binascii.a2b_qp(data, header=False

Convert a block of quoted-printable data back to binary and rerevolve the binaryinformation. More than one line may be passed at a time. If the optional argumentheader is current and true, underscores will be decoded as spaces.

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

Convert binary information to a line(s) of ASCII personalities in quoted-printableencoding. The rerotate value is the converted line(s). If the optional argumentquotetabs is current and also true, all tabs and spaces will certainly be encoded. If theoptional discussion istext is existing and also true, newlines are not encoded buttrailing whitespace will certainly be encoded. If the optional discussion header isexisting and true, spaces will be encoded as underscores per RFC 1522. If theoptional debate header is current and also false, newline personalities will beencoded as well; otherwise linefeed conversion could corrupt the binary datastream.


Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.The string need to contain a finish number of binary bytes, or (in case of thelast percent of the binhex4 data) have the remaining bits zero.


Perdevelop RLE-decompression on the information, as per the binhex4 conventional. Thealgorithm supplies 0x90 after a byte as a repeat indicator, adhered to by a count.A count of 0 mentions a byte worth of 0x90. The routine retransforms thedecompressed information, unless data input data ends in an orphaned repeat indicator,in which case the Infinish exemption is increased.


Percreate hexbin4 binary-to-ASCII translation and return the resulting string. Thedispute must already be RLE-coded, and have actually a size divisible by 3 (exceptmaybe the last fragment).

binascii.crc_hqx(data, value

Compute a 16-little bit CRC worth of data, starting via value as theinitial CRC, and also return the outcome. This supplies the CRC-CCITT polynomialx16 + x12 + x5 + 1, regularly stood for as0x1021. This CRC is provided in the binhex4 format.

See more: Book Review: Sensible Shoes Book Review : Sensible Shoes, Sensible Shoes, Book 1: Sharon Garlough Brown

binascii.crc32(data<, value>)¶

Compute CRC-32, the 32-little bit checkamount of data, starting with aninitial CRC of value. The default initial CRC is zero. The algorithmis continual through the ZIP file checkamount. Due to the fact that the algorithm is designed forusage as a checkamount algorithm, it is not suitable for use as a general hashalgorithm. 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 variation 3.0: The result is constantly unsigned.To geneprice the exact same numeric worth throughout all versions andplatforms, usage crc32(data) & 0xffffffff.

Rerotate the hexadecimal representation of the binary data. Eextremely byte ofdata is converted into the matching 2-digit hex representation. Thewent back bytes object is therefore twice as lengthy as the length of data.

Comparable usability (but returning a message string) is likewise convenientlyeasily accessible using the bytes.hex() strategy.

If sep is stated, it should be a single character str or bytes object.It will certainly be put in the output after eexceptionally bytes_per_sep input bytes.Separator placement is counted from the ideal end of the output by default,if you wish to count from the left, supply an unfavorable bytes_per_sep value.


Return the binary data represented by the hexadecimal string hexstr. Thisattribute is the inverse of b2a_hex(). hexstr must contain an also numberof hexadecimal digits (which deserve to be upper or reduced case), otherwise anError exception is raised.

Comparable functionality (accepting only message string arguments, however moreliberal towards whitespace) is additionally easily accessible using thebytes.fromhex() class approach.

exception binascii.Error¶

Exception raised on errors. These are typically programming errors.

exemption binascii.Incomplete¶

Exception increased on incomplete data. These are usually not programming errors,however might be taken on by analysis a little more data and also trying aobtain.

See also

Module base64

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

Module binhex

Support for the binhex format provided on the Macintosh.

Module uu

Support for UU encoding supplied on Unix.

Module quopri

Support for quoted-printable encoding used in MIME email messperiods.

Previous topic

binhex — Encode and decode binhex4 files

Next topic

quopri — Encode and decode MIME quoted-printable data

This Page