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