blob: 0cd18b1740e0c2a4effa7f7e08d19fb585e5fe01 [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
8
9.. index::
10 module: uu
11 module: base64
12 module: binhex
13
14The :mod:`binascii` module contains a number of methods to convert between
15binary and various ASCII-encoded binary representations. Normally, you will not
16use these functions directly but use wrapper modules like :mod:`uu`,
17:mod:`base64`, or :mod:`binhex` instead. The :mod:`binascii` module contains
18low-level functions written in C for greater speed that are used by the
19higher-level modules.
20
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000021.. note::
22
Antoine Pitrou08316762011-12-20 13:58:41 +010023 ``a2b_*`` functions accept Unicode strings containing only ASCII characters.
Ezio Melottic228e962013-05-04 18:06:34 +030024 Other functions only accept :term:`bytes-like object`\ s (such as
25 :class:`bytes`, :class:`bytearray` and other objects that support the buffer
26 protocol).
Antoine Pitrou08316762011-12-20 13:58:41 +010027
28 .. versionchanged:: 3.3
29 ASCII-only unicode strings are now accepted by the ``a2b_*`` functions.
30
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000031
Georg Brandl116aa622007-08-15 14:28:22 +000032The :mod:`binascii` module defines the following functions:
33
34
35.. function:: a2b_uu(string)
36
37 Convert a single line of uuencoded data back to binary and return the binary
38 data. Lines normally contain 45 (binary) bytes, except for the last line. Line
39 data may be followed by whitespace.
40
41
42.. function:: b2a_uu(data)
43
44 Convert binary data to a line of ASCII characters, the return value is the
45 converted line, including a newline char. The length of *data* should be at most
46 45.
47
48
49.. function:: a2b_base64(string)
50
51 Convert a block of base64 data back to binary and return the binary data. More
52 than one line may be passed at a time.
53
54
Victor Stinnere84c9762015-10-11 11:01:02 +020055.. function:: b2a_base64(data, \*, newline=True)
Georg Brandl116aa622007-08-15 14:28:22 +000056
57 Convert binary data to a line of ASCII characters in base64 coding. The return
Victor Stinnere84c9762015-10-11 11:01:02 +020058 value is the converted line, including a newline char if *newline* is
59 true. The length of *data* should be at most 57 to adhere to the
60 base64 standard.
61
62
63 .. versionchanged:: 3.6
64 Added the *newline* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66
Martin Panterbf19d162015-09-09 01:01:13 +000067.. function:: a2b_qp(data, header=False)
Georg Brandl116aa622007-08-15 14:28:22 +000068
69 Convert a block of quoted-printable data back to binary and return the binary
70 data. More than one line may be passed at a time. If the optional argument
71 *header* is present and true, underscores will be decoded as spaces.
72
73
Georg Brandlb868a662009-04-02 02:56:10 +000074.. function:: b2a_qp(data, quotetabs=False, istext=True, header=False)
Georg Brandl116aa622007-08-15 14:28:22 +000075
76 Convert binary data to a line(s) of ASCII characters in quoted-printable
77 encoding. The return value is the converted line(s). If the optional argument
78 *quotetabs* is present and true, all tabs and spaces will be encoded. If the
79 optional argument *istext* is present and true, newlines are not encoded but
80 trailing whitespace will be encoded. If the optional argument *header* is
81 present and true, spaces will be encoded as underscores per RFC1522. If the
82 optional argument *header* is present and false, newline characters will be
83 encoded as well; otherwise linefeed conversion might corrupt the binary data
84 stream.
85
86
87.. function:: a2b_hqx(string)
88
89 Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.
90 The string should contain a complete number of binary bytes, or (in case of the
91 last portion of the binhex4 data) have the remaining bits zero.
92
93
94.. function:: rledecode_hqx(data)
95
96 Perform RLE-decompression on the data, as per the binhex4 standard. The
97 algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count.
98 A count of ``0`` specifies a byte value of ``0x90``. The routine returns the
99 decompressed data, unless data input data ends in an orphaned repeat indicator,
100 in which case the :exc:`Incomplete` exception is raised.
101
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000102 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +0000103 Accept only bytestring or bytearray objects as input.
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000104
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106.. function:: rlecode_hqx(data)
107
108 Perform binhex4 style RLE-compression on *data* and return the result.
109
110
111.. function:: b2a_hqx(data)
112
113 Perform hexbin4 binary-to-ASCII translation and return the resulting string. The
114 argument should already be RLE-coded, and have a length divisible by 3 (except
115 possibly the last fragment).
116
117
Martin Panterb82032f2015-12-11 05:19:29 +0000118.. function:: crc_hqx(data, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Martin Panterb82032f2015-12-11 05:19:29 +0000120 Compute the binhex4 crc value of *data*, starting with *value* as the
121 initial crc, and return the result.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
Martin Panterb82032f2015-12-11 05:19:29 +0000124.. function:: crc32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +0000125
Martin Panterb82032f2015-12-11 05:19:29 +0000126 Compute CRC-32, the 32-bit checksum of *data*, starting with an
127 initial CRC of *value*. The default initial CRC is zero. The algorithm
Georg Brandl116aa622007-08-15 14:28:22 +0000128 is consistent with the ZIP file checksum. Since the algorithm is designed for
129 use as a checksum algorithm, it is not suitable for use as a general hash
130 algorithm. Use as follows::
131
Georg Brandlede6c2a2010-01-05 10:22:04 +0000132 print(binascii.crc32(b"hello world"))
Georg Brandl116aa622007-08-15 14:28:22 +0000133 # Or, in two pieces:
Georg Brandlede6c2a2010-01-05 10:22:04 +0000134 crc = binascii.crc32(b"hello")
Martin Panterb82032f2015-12-11 05:19:29 +0000135 crc = binascii.crc32(b" world", crc)
Georg Brandlede6c2a2010-01-05 10:22:04 +0000136 print('crc32 = {:#010x}'.format(crc))
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000137
Martin Panterb82032f2015-12-11 05:19:29 +0000138 .. versionchanged:: 3.0
139 The result is always unsigned.
140 To generate the same numeric value across all Python versions and
141 platforms, use ``crc32(data) & 0xffffffff``.
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000142
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144.. function:: b2a_hex(data)
145 hexlify(data)
146
147 Return the hexadecimal representation of the binary *data*. Every byte of
148 *data* is converted into the corresponding 2-digit hex representation. The
R David Murray5fdb64b2013-11-03 13:21:38 -0500149 returned bytes object is therefore twice as long as the length of *data*.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151
152.. function:: a2b_hex(hexstr)
153 unhexlify(hexstr)
154
155 Return the binary data represented by the hexadecimal string *hexstr*. This
156 function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
157 of hexadecimal digits (which can be upper or lower case), otherwise a
158 :exc:`TypeError` is raised.
159
160
161.. exception:: Error
162
163 Exception raised on errors. These are usually programming errors.
164
165
166.. exception:: Incomplete
167
168 Exception raised on incomplete data. These are usually not programming errors,
169 but may be handled by reading a little more data and trying again.
170
171
172.. seealso::
173
174 Module :mod:`base64`
175 Support for base64 encoding used in MIME email messages.
176
177 Module :mod:`binhex`
178 Support for the binhex format used on the Macintosh.
179
180 Module :mod:`uu`
181 Support for UU encoding used on Unix.
182
183 Module :mod:`quopri`
184 Support for quoted-printable encoding used in MIME email messages.