• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2:mod:`binascii` --- Convert between binary and ASCII
3====================================================
4
5.. module:: binascii
6   :synopsis: Tools for converting between binary and various ASCII-encoded binary
7              representations.
8
9
10.. index::
11   module: uu
12   module: base64
13   module: binhex
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
22The :mod:`binascii` module defines the following functions:
23
24
25.. function:: a2b_uu(string)
26
27   Convert a single line of uuencoded data back to binary and return the binary
28   data. Lines normally contain 45 (binary) bytes, except for the last line. Line
29   data may be followed by whitespace.
30
31
32.. function:: b2a_uu(data)
33
34   Convert binary data to a line of ASCII characters, the return value is the
35   converted line, including a newline char. The length of *data* should be at most
36   45.
37
38
39.. function:: a2b_base64(string)
40
41   Convert a block of base64 data back to binary and return the binary data. More
42   than one line may be passed at a time.
43
44
45.. function:: b2a_base64(data)
46
47   Convert binary data to a line of ASCII characters in base64 coding. The return
48   value is the converted line, including a newline char.  The newline is
49   added because the original use case for this function was to feed it a
50   series of 57 byte input lines to get output lines that conform to the
51   MIME-base64 standard.  Otherwise the output conforms to :rfc:`3548`.
52
53
54.. function:: a2b_qp(string[, header])
55
56   Convert a block of quoted-printable data back to binary and return the binary
57   data. More than one line may be passed at a time. If the optional argument
58   *header* is present and true, underscores will be decoded as spaces.
59
60
61.. function:: b2a_qp(data[, quotetabs, istext, header])
62
63   Convert binary data to a line(s) of ASCII characters in quoted-printable
64   encoding.  The return value is the converted line(s). If the optional argument
65   *quotetabs* is present and true, all tabs and spaces will be encoded.   If the
66   optional argument *istext* is present and true, newlines are not encoded but
67   trailing whitespace will be encoded. If the optional argument *header* is
68   present and true, spaces will be encoded as underscores per RFC1522. If the
69   optional argument *header* is present and false, newline characters will be
70   encoded as well; otherwise linefeed conversion might corrupt the binary data
71   stream.
72
73
74.. function:: a2b_hqx(string)
75
76   Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.
77   The string should contain a complete number of binary bytes, or (in case of the
78   last portion of the binhex4 data) have the remaining bits zero.
79
80
81.. function:: rledecode_hqx(data)
82
83   Perform RLE-decompression on the data, as per the binhex4 standard. The
84   algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count.
85   A count of ``0`` specifies a byte value of ``0x90``. The routine returns the
86   decompressed data, unless data input data ends in an orphaned repeat indicator,
87   in which case the :exc:`Incomplete` exception is raised.
88
89
90.. function:: rlecode_hqx(data)
91
92   Perform binhex4 style RLE-compression on *data* and return the result.
93
94
95.. function:: b2a_hqx(data)
96
97   Perform hexbin4 binary-to-ASCII translation and return the resulting string. The
98   argument should already be RLE-coded, and have a length divisible by 3 (except
99   possibly the last fragment).
100
101
102.. function:: crc_hqx(data, crc)
103
104   Compute a 16-bit CRC value of *data*, starting with an initial *crc* and
105   returning the result.  This uses the CRC-CCITT polynomial
106   *x*:sup:`16` + *x*:sup:`12` + *x*:sup:`5` + 1, often represented as
107   0x1021.  This CRC is used in the binhex4 format.
108
109
110.. function:: crc32(data[, crc])
111
112   Compute CRC-32, the 32-bit checksum of data, starting with an initial crc.  This
113   is consistent with the ZIP file checksum.  Since the algorithm is designed for
114   use as a checksum algorithm, it is not suitable for use as a general hash
115   algorithm.  Use as follows::
116
117      print binascii.crc32("hello world")
118      # Or, in two pieces:
119      crc = binascii.crc32("hello")
120      crc = binascii.crc32(" world", crc) & 0xffffffff
121      print 'crc32 = 0x%08x' % crc
122
123.. note::
124   To generate the same numeric value across all Python versions and
125   platforms use crc32(data) & 0xffffffff.  If you are only using
126   the checksum in packed binary format this is not necessary as the
127   return value is the correct 32bit binary representation
128   regardless of sign.
129
130.. versionchanged:: 2.6
131   The return value is in the range [-2**31, 2**31-1]
132   regardless of platform.  In the past the value would be signed on
133   some platforms and unsigned on others.  Use & 0xffffffff on the
134   value if you want it to match Python 3 behavior.
135
136.. versionchanged:: 3.0
137   The return value is unsigned and in the range [0, 2**32-1]
138   regardless of platform.
139
140
141.. function:: b2a_hex(data)
142              hexlify(data)
143
144   Return the hexadecimal representation of the binary *data*.  Every byte of
145   *data* is converted into the corresponding 2-digit hex representation.  The
146   resulting string is therefore twice as long as the length of *data*.
147
148
149.. function:: a2b_hex(hexstr)
150              unhexlify(hexstr)
151
152   Return the binary data represented by the hexadecimal string *hexstr*.  This
153   function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
154   of hexadecimal digits (which can be upper or lower case), otherwise a
155   :exc:`TypeError` is raised.
156
157
158.. exception:: Error
159
160   Exception raised on errors. These are usually programming errors.
161
162
163.. exception:: Incomplete
164
165   Exception raised on incomplete data. These are usually not programming errors,
166   but may be handled by reading a little more data and trying again.
167
168
169.. seealso::
170
171   Module :mod:`base64`
172      Support for RFC compliant base64-style encoding in base 16, 32, and 64.
173
174   Module :mod:`binhex`
175      Support for the binhex format used on the Macintosh.
176
177   Module :mod:`uu`
178      Support for UU encoding used on Unix.
179
180   Module :mod:`quopri`
181      Support for quoted-printable encoding used in MIME email messages.
182
183