blob: baf430db898cce643c1fc0bcb55c975b0f2bf358 [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.
24 Other functions only accept bytes and bytes-compatible objects (such as
25 bytearray objects and other objects implementing the buffer API).
26
27 .. versionchanged:: 3.3
28 ASCII-only unicode strings are now accepted by the ``a2b_*`` functions.
29
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000030
Georg Brandl116aa622007-08-15 14:28:22 +000031The :mod:`binascii` module defines the following functions:
32
33
34.. function:: a2b_uu(string)
35
36 Convert a single line of uuencoded data back to binary and return the binary
37 data. Lines normally contain 45 (binary) bytes, except for the last line. Line
38 data may be followed by whitespace.
39
40
41.. function:: b2a_uu(data)
42
43 Convert binary data to a line of ASCII characters, the return value is the
44 converted line, including a newline char. The length of *data* should be at most
45 45.
46
47
48.. function:: a2b_base64(string)
49
50 Convert a block of base64 data back to binary and return the binary data. More
51 than one line may be passed at a time.
52
53
54.. function:: b2a_base64(data)
55
56 Convert binary data to a line of ASCII characters in base64 coding. The return
57 value is the converted line, including a newline char. The length of *data*
58 should be at most 57 to adhere to the base64 standard.
59
60
Georg Brandlb868a662009-04-02 02:56:10 +000061.. function:: a2b_qp(string, header=False)
Georg Brandl116aa622007-08-15 14:28:22 +000062
63 Convert a block of quoted-printable data back to binary and return the binary
64 data. More than one line may be passed at a time. If the optional argument
65 *header* is present and true, underscores will be decoded as spaces.
66
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000067 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +000068 Accept only bytestring or bytearray objects as input.
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000069
Georg Brandl116aa622007-08-15 14:28:22 +000070
Georg Brandlb868a662009-04-02 02:56:10 +000071.. function:: b2a_qp(data, quotetabs=False, istext=True, header=False)
Georg Brandl116aa622007-08-15 14:28:22 +000072
73 Convert binary data to a line(s) of ASCII characters in quoted-printable
74 encoding. The return value is the converted line(s). If the optional argument
75 *quotetabs* is present and true, all tabs and spaces will be encoded. If the
76 optional argument *istext* is present and true, newlines are not encoded but
77 trailing whitespace will be encoded. If the optional argument *header* is
78 present and true, spaces will be encoded as underscores per RFC1522. If the
79 optional argument *header* is present and false, newline characters will be
80 encoded as well; otherwise linefeed conversion might corrupt the binary data
81 stream.
82
83
84.. function:: a2b_hqx(string)
85
86 Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.
87 The string should contain a complete number of binary bytes, or (in case of the
88 last portion of the binhex4 data) have the remaining bits zero.
89
90
91.. function:: rledecode_hqx(data)
92
93 Perform RLE-decompression on the data, as per the binhex4 standard. The
94 algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count.
95 A count of ``0`` specifies a byte value of ``0x90``. The routine returns the
96 decompressed data, unless data input data ends in an orphaned repeat indicator,
97 in which case the :exc:`Incomplete` exception is raised.
98
Florent Xiclunaf1046ca2010-07-27 21:20:15 +000099 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +0000100 Accept only bytestring or bytearray objects as input.
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000101
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103.. function:: rlecode_hqx(data)
104
105 Perform binhex4 style RLE-compression on *data* and return the result.
106
107
108.. function:: b2a_hqx(data)
109
110 Perform hexbin4 binary-to-ASCII translation and return the resulting string. The
111 argument should already be RLE-coded, and have a length divisible by 3 (except
112 possibly the last fragment).
113
114
115.. function:: crc_hqx(data, crc)
116
117 Compute the binhex4 crc value of *data*, starting with an initial *crc* and
118 returning the result.
119
120
121.. function:: crc32(data[, crc])
122
123 Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This
124 is consistent with the ZIP file checksum. Since the algorithm is designed for
125 use as a checksum algorithm, it is not suitable for use as a general hash
126 algorithm. Use as follows::
127
Georg Brandlede6c2a2010-01-05 10:22:04 +0000128 print(binascii.crc32(b"hello world"))
Georg Brandl116aa622007-08-15 14:28:22 +0000129 # Or, in two pieces:
Georg Brandlede6c2a2010-01-05 10:22:04 +0000130 crc = binascii.crc32(b"hello")
131 crc = binascii.crc32(b" world", crc) & 0xffffffff
132 print('crc32 = {:#010x}'.format(crc))
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000133
134.. note::
135 To generate the same numeric value across all Python versions and
136 platforms use crc32(data) & 0xffffffff. If you are only using
137 the checksum in packed binary format this is not necessary as the
Gregory P. Smithfa6cf392009-02-01 00:30:50 +0000138 return value is the correct 32bit binary representation
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000139 regardless of sign.
140
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142.. function:: b2a_hex(data)
143 hexlify(data)
144
145 Return the hexadecimal representation of the binary *data*. Every byte of
146 *data* is converted into the corresponding 2-digit hex representation. The
147 resulting string is therefore twice as long as the length of *data*.
148
149
150.. function:: a2b_hex(hexstr)
151 unhexlify(hexstr)
152
153 Return the binary data represented by the hexadecimal string *hexstr*. This
154 function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
155 of hexadecimal digits (which can be upper or lower case), otherwise a
156 :exc:`TypeError` is raised.
157
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000158 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +0000159 Accept only bytestring or bytearray objects as input.
Florent Xiclunaf1046ca2010-07-27 21:20:15 +0000160
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162.. exception:: Error
163
164 Exception raised on errors. These are usually programming errors.
165
166
167.. exception:: Incomplete
168
169 Exception raised on incomplete data. These are usually not programming errors,
170 but may be handled by reading a little more data and trying again.
171
172
173.. seealso::
174
175 Module :mod:`base64`
176 Support for base64 encoding used in MIME email messages.
177
178 Module :mod:`binhex`
179 Support for the binhex format used on the Macintosh.
180
181 Module :mod:`uu`
182 Support for UU encoding used on Unix.
183
184 Module :mod:`quopri`
185 Support for quoted-printable encoding used in MIME email messages.