blob: 2c0c1bce5d7f8f9a0f18f0be815a0c4c5605b6df [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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
Georg Brandl116aa622007-08-15 14:28:22 +00008.. index::
9 module: uu
10 module: base64
11 module: binhex
12
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040013--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015The :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
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000022.. note::
23
Antoine Pitrou08316762011-12-20 13:58:41 +010024 ``a2b_*`` functions accept Unicode strings containing only ASCII characters.
Serhiy Storchakae5ea1ab2016-05-18 13:54:54 +030025 Other functions only accept :term:`bytes-like objects <bytes-like object>` (such as
Ezio Melottic228e962013-05-04 18:06:34 +030026 :class:`bytes`, :class:`bytearray` and other objects that support the buffer
27 protocol).
Antoine Pitrou08316762011-12-20 13:58:41 +010028
29 .. versionchanged:: 3.3
30 ASCII-only unicode strings are now accepted by the ``a2b_*`` functions.
31
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000032
Georg Brandl116aa622007-08-15 14:28:22 +000033The :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
Xiang Zhang13f1f422017-05-03 11:16:21 +080043.. function:: b2a_uu(data, *, backtick=False)
Georg Brandl116aa622007-08-15 14:28:22 +000044
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
Xiang Zhang13f1f422017-05-03 11:16:21 +080047 45. If *backtick* is true, zeros are represented by ``'`'`` instead of spaces.
48
49 .. versionchanged:: 3.7
50 Added the *backtick* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000051
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
Xiang Zhang13f1f422017-05-03 11:16:21 +080059.. function:: b2a_base64(data, *, newline=True)
Georg Brandl116aa622007-08-15 14:28:22 +000060
61 Convert binary data to a line of ASCII characters in base64 coding. The return
Victor Stinnere84c9762015-10-11 11:01:02 +020062 value is the converted line, including a newline char if *newline* is
R David Murrayd9919632015-12-13 18:11:07 -050063 true. The output of this function conforms to :rfc:`3548`.
Victor Stinnere84c9762015-10-11 11:01:02 +020064
65 .. versionchanged:: 3.6
66 Added the *newline* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
Martin Panterbf19d162015-09-09 01:01:13 +000069.. function:: a2b_qp(data, header=False)
Georg Brandl116aa622007-08-15 14:28:22 +000070
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
Georg Brandlb868a662009-04-02 02:56:10 +000076.. function:: b2a_qp(data, quotetabs=False, istext=True, header=False)
Georg Brandl116aa622007-08-15 14:28:22 +000077
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
Serhiy Storchaka0a36ac12018-05-31 07:39:00 +030083 present and true, spaces will be encoded as underscores per :rfc:`1522`. If the
Georg Brandl116aa622007-08-15 14:28:22 +000084 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
Victor Stinnerbeea26b2020-01-22 20:44:22 +010095 .. deprecated:: 3.9
96
Georg Brandl116aa622007-08-15 14:28:22 +000097
98.. function:: rledecode_hqx(data)
99
100 Perform RLE-decompression on the data, as per the binhex4 standard. The
101 algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count.
102 A count of ``0`` specifies a byte value of ``0x90``. The routine returns the
103 decompressed data, unless data input data ends in an orphaned repeat indicator,
104 in which case the :exc:`Incomplete` exception is raised.
105
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000106 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +0000107 Accept only bytestring or bytearray objects as input.
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000108
Victor Stinnerbeea26b2020-01-22 20:44:22 +0100109 .. deprecated:: 3.9
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112.. function:: rlecode_hqx(data)
113
114 Perform binhex4 style RLE-compression on *data* and return the result.
115
Victor Stinnerbeea26b2020-01-22 20:44:22 +0100116 .. deprecated:: 3.9
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119.. function:: b2a_hqx(data)
120
121 Perform hexbin4 binary-to-ASCII translation and return the resulting string. The
122 argument should already be RLE-coded, and have a length divisible by 3 (except
123 possibly the last fragment).
124
Victor Stinnerbeea26b2020-01-22 20:44:22 +0100125 .. deprecated:: 3.9
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Martin Panterb82032f2015-12-11 05:19:29 +0000128.. function:: crc_hqx(data, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Martin Panter3310e142016-12-24 07:36:44 +0000130 Compute a 16-bit CRC value of *data*, starting with *value* as the
131 initial CRC, and return the result. This uses the CRC-CCITT polynomial
132 *x*:sup:`16` + *x*:sup:`12` + *x*:sup:`5` + 1, often represented as
133 0x1021. This CRC is used in the binhex4 format.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
Martin Panterb82032f2015-12-11 05:19:29 +0000136.. function:: crc32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Martin Panterb82032f2015-12-11 05:19:29 +0000138 Compute CRC-32, the 32-bit checksum of *data*, starting with an
139 initial CRC of *value*. The default initial CRC is zero. The algorithm
Georg Brandl116aa622007-08-15 14:28:22 +0000140 is consistent with the ZIP file checksum. Since the algorithm is designed for
141 use as a checksum algorithm, it is not suitable for use as a general hash
142 algorithm. Use as follows::
143
Georg Brandlede6c2a2010-01-05 10:22:04 +0000144 print(binascii.crc32(b"hello world"))
Georg Brandl116aa622007-08-15 14:28:22 +0000145 # Or, in two pieces:
Georg Brandlede6c2a2010-01-05 10:22:04 +0000146 crc = binascii.crc32(b"hello")
Martin Panterb82032f2015-12-11 05:19:29 +0000147 crc = binascii.crc32(b" world", crc)
Georg Brandlede6c2a2010-01-05 10:22:04 +0000148 print('crc32 = {:#010x}'.format(crc))
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000149
Martin Panterb82032f2015-12-11 05:19:29 +0000150 .. versionchanged:: 3.0
151 The result is always unsigned.
152 To generate the same numeric value across all Python versions and
153 platforms, use ``crc32(data) & 0xffffffff``.
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000154
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700156.. function:: b2a_hex(data[, sep[, bytes_per_sep=1]])
157 hexlify(data[, sep[, bytes_per_sep=1]])
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159 Return the hexadecimal representation of the binary *data*. Every byte of
160 *data* is converted into the corresponding 2-digit hex representation. The
R David Murray5fdb64b2013-11-03 13:21:38 -0500161 returned bytes object is therefore twice as long as the length of *data*.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Raymond Hettinger56edf3a2018-12-25 17:53:36 -0800163 Similar functionality (but returning a text string) is also conveniently
164 accessible using the :meth:`bytes.hex` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700166 If *sep* is specified, it must be a single character str or bytes object.
167 It will be inserted in the output after every *bytes_per_sep* input bytes.
168 Separator placement is counted from the right end of the output by default,
169 if you wish to count from the left, supply a negative *bytes_per_sep* value.
170
171 >>> import binascii
172 >>> binascii.b2a_hex(b'\xb9\x01\xef')
173 b'b901ef'
174 >>> binascii.hexlify(b'\xb9\x01\xef', '-')
175 b'b9-01-ef'
176 >>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2)
177 b'b9_01ef'
178 >>> binascii.b2a_hex(b'\xb9\x01\xef', b' ', -2)
179 b'b901 ef'
180
181 .. versionchanged:: 3.8
182 The *sep* and *bytes_per_sep* parameters were added.
183
Georg Brandl116aa622007-08-15 14:28:22 +0000184.. function:: a2b_hex(hexstr)
185 unhexlify(hexstr)
186
187 Return the binary data represented by the hexadecimal string *hexstr*. This
188 function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
Martin Panter58dd7642016-05-29 00:48:54 +0000189 of hexadecimal digits (which can be upper or lower case), otherwise an
190 :exc:`Error` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000191
Raymond Hettinger56edf3a2018-12-25 17:53:36 -0800192 Similar functionality (accepting only text string arguments, but more
193 liberal towards whitespace) is also accessible using the
194 :meth:`bytes.fromhex` class method.
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196.. exception:: Error
197
198 Exception raised on errors. These are usually programming errors.
199
200
201.. exception:: Incomplete
202
203 Exception raised on incomplete data. These are usually not programming errors,
204 but may be handled by reading a little more data and trying again.
205
206
207.. seealso::
208
209 Module :mod:`base64`
R David Murray2b4f47e2015-12-13 18:04:27 -0500210 Support for RFC compliant base64-style encoding in base 16, 32, 64,
211 and 85.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213 Module :mod:`binhex`
214 Support for the binhex format used on the Macintosh.
215
216 Module :mod:`uu`
217 Support for UU encoding used on Unix.
218
219 Module :mod:`quopri`
220 Support for quoted-printable encoding used in MIME email messages.