1:mod:`binascii` --- Convert between binary and ASCII
2====================================================
3
4.. module:: binascii
5   :synopsis: Tools for converting between binary and various ASCII-encoded binary
6              representations.
7
8.. index::
9   module: uu
10   module: base64
11   module: binhex
12
13--------------
14
15The :mod:`binascii` module contains a number of methods to convert between
16binary and various ASCII-encoded binary representations. Normally, you will not
17use these functions directly but use wrapper modules like :mod:`uu`,
18:mod:`base64`, or :mod:`binhex` instead. The :mod:`binascii` module contains
19low-level functions written in C for greater speed that are used by the
20higher-level modules.
21
22.. note::
23
24   ``a2b_*`` functions accept Unicode strings containing only ASCII characters.
25   Other functions only accept :term:`bytes-like objects <bytes-like object>` (such as
26   :class:`bytes`, :class:`bytearray` and other objects that support the buffer
27   protocol).
28
29   .. versionchanged:: 3.3
30      ASCII-only unicode strings are now accepted by the ``a2b_*`` functions.
31
32
33The :mod:`binascii` module defines the following functions:
34
35
36.. function:: a2b_uu(string)
37
38   Convert a single line of uuencoded data back to binary and return the binary
39   data. Lines normally contain 45 (binary) bytes, except for the last line. Line
40   data may be followed by whitespace.
41
42
43.. function:: b2a_uu(data, *, backtick=False)
44
45   Convert binary data to a line of ASCII characters, the return value is the
46   converted line, including a newline char. The length of *data* should be at most
47   45. If *backtick* is true, zeros are represented by ``'`'`` instead of spaces.
48
49   .. versionchanged:: 3.7
50      Added the *backtick* parameter.
51
52
53.. function:: a2b_base64(string)
54
55   Convert a block of base64 data back to binary and return the binary data. More
56   than one line may be passed at a time.
57
58
59.. function:: b2a_base64(data, *, newline=True)
60
61   Convert binary data to a line of ASCII characters in base64 coding. The return
62   value is the converted line, including a newline char if *newline* is
63   true.  The output of this function conforms to :rfc:`3548`.
64
65   .. versionchanged:: 3.6
66      Added the *newline* parameter.
67
68
69.. function:: a2b_qp(data, header=False)
70
71   Convert a block of quoted-printable data back to binary and return the binary
72   data. More than one line may be passed at a time. If the optional argument
73   *header* is present and true, underscores will be decoded as spaces.
74
75
76.. function:: b2a_qp(data, quotetabs=False, istext=True, header=False)
77
78   Convert binary data to a line(s) of ASCII characters in quoted-printable
79   encoding.  The return value is the converted line(s). If the optional argument
80   *quotetabs* is present and true, all tabs and spaces will be encoded.   If the
81   optional argument *istext* is present and true, newlines are not encoded but
82   trailing whitespace will be encoded. If the optional argument *header* is
83   present and true, spaces will be encoded as underscores per :rfc:`1522`. If the
84   optional argument *header* is present and false, newline characters will be
85   encoded as well; otherwise linefeed conversion might corrupt the binary data
86   stream.
87
88
89.. function:: a2b_hqx(string)
90
91   Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.
92   The string should contain a complete number of binary bytes, or (in case of the
93   last portion of the binhex4 data) have the remaining bits zero.
94
95
96.. function:: rledecode_hqx(data)
97
98   Perform RLE-decompression on the data, as per the binhex4 standard. The
99   algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count.
100   A count of ``0`` specifies a byte value of ``0x90``. The routine returns the
101   decompressed data, unless data input data ends in an orphaned repeat indicator,
102   in which case the :exc:`Incomplete` exception is raised.
103
104   .. versionchanged:: 3.2
105      Accept only bytestring or bytearray objects as input.
106
107
108.. function:: rlecode_hqx(data)
109
110   Perform binhex4 style RLE-compression on *data* and return the result.
111
112
113.. function:: b2a_hqx(data)
114
115   Perform hexbin4 binary-to-ASCII translation and return the resulting string. The
116   argument should already be RLE-coded, and have a length divisible by 3 (except
117   possibly the last fragment).
118
119
120.. function:: crc_hqx(data, value)
121
122   Compute a 16-bit CRC value of *data*, starting with *value* as the
123   initial CRC, and return the result.  This uses the CRC-CCITT polynomial
124   *x*:sup:`16` + *x*:sup:`12` + *x*:sup:`5` + 1, often represented as
125   0x1021.  This CRC is used in the binhex4 format.
126
127
128.. function:: crc32(data[, value])
129
130   Compute CRC-32, the 32-bit checksum of *data*, starting with an
131   initial CRC of *value*.  The default initial CRC is zero.  The algorithm
132   is consistent with the ZIP file checksum.  Since the algorithm is designed for
133   use as a checksum algorithm, it is not suitable for use as a general hash
134   algorithm.  Use as follows::
135
136      print(binascii.crc32(b"hello world"))
137      # Or, in two pieces:
138      crc = binascii.crc32(b"hello")
139      crc = binascii.crc32(b" world", crc)
140      print('crc32 = {:#010x}'.format(crc))
141
142   .. versionchanged:: 3.0
143      The result is always unsigned.
144      To generate the same numeric value across all Python versions and
145      platforms, use ``crc32(data) & 0xffffffff``.
146
147
148.. function:: b2a_hex(data[, sep[, bytes_per_sep=1]])
149              hexlify(data[, sep[, bytes_per_sep=1]])
150
151   Return the hexadecimal representation of the binary *data*.  Every byte of
152   *data* is converted into the corresponding 2-digit hex representation.  The
153   returned bytes object is therefore twice as long as the length of *data*.
154
155   Similar functionality (but returning a text string) is also conveniently
156   accessible using the :meth:`bytes.hex` method.
157
158   If *sep* is specified, it must be a single character str or bytes object.
159   It will be inserted in the output after every *bytes_per_sep* input bytes.
160   Separator placement is counted from the right end of the output by default,
161   if you wish to count from the left, supply a negative *bytes_per_sep* value.
162
163      >>> import binascii
164      >>> binascii.b2a_hex(b'\xb9\x01\xef')
165      b'b901ef'
166      >>> binascii.hexlify(b'\xb9\x01\xef', '-')
167      b'b9-01-ef'
168      >>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2)
169      b'b9_01ef'
170      >>> binascii.b2a_hex(b'\xb9\x01\xef', b' ', -2)
171      b'b901 ef'
172
173   .. versionchanged:: 3.8
174      The *sep* and *bytes_per_sep* parameters were added.
175
176.. function:: a2b_hex(hexstr)
177              unhexlify(hexstr)
178
179   Return the binary data represented by the hexadecimal string *hexstr*.  This
180   function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
181   of hexadecimal digits (which can be upper or lower case), otherwise an
182   :exc:`Error` exception is raised.
183
184   Similar functionality (accepting only text string arguments, but more
185   liberal towards whitespace) is also accessible using the
186   :meth:`bytes.fromhex` class method.
187
188.. exception:: Error
189
190   Exception raised on errors. These are usually programming errors.
191
192
193.. exception:: Incomplete
194
195   Exception raised on incomplete data. These are usually not programming errors,
196   but may be handled by reading a little more data and trying again.
197
198
199.. seealso::
200
201   Module :mod:`base64`
202      Support for RFC compliant base64-style encoding in base 16, 32, 64,
203      and 85.
204
205   Module :mod:`binhex`
206      Support for the binhex format used on the Macintosh.
207
208   Module :mod:`uu`
209      Support for UU encoding used on Unix.
210
211   Module :mod:`quopri`
212      Support for quoted-printable encoding used in MIME email messages.
213