Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
| 14 | The :mod:`binascii` module contains a number of methods to convert between |
| 15 | binary and various ASCII-encoded binary representations. Normally, you will not |
| 16 | use these functions directly but use wrapper modules like :mod:`uu`, |
| 17 | :mod:`base64`, or :mod:`binhex` instead. The :mod:`binascii` module contains |
| 18 | low-level functions written in C for greater speed that are used by the |
| 19 | higher-level modules. |
| 20 | |
| 21 | The :mod:`binascii` module defines the following functions: |
| 22 | |
| 23 | |
| 24 | .. function:: a2b_uu(string) |
| 25 | |
| 26 | Convert a single line of uuencoded data back to binary and return the binary |
| 27 | data. Lines normally contain 45 (binary) bytes, except for the last line. Line |
| 28 | data may be followed by whitespace. |
| 29 | |
| 30 | |
| 31 | .. function:: b2a_uu(data) |
| 32 | |
| 33 | Convert binary data to a line of ASCII characters, the return value is the |
| 34 | converted line, including a newline char. The length of *data* should be at most |
| 35 | 45. |
| 36 | |
| 37 | |
| 38 | .. function:: a2b_base64(string) |
| 39 | |
| 40 | Convert a block of base64 data back to binary and return the binary data. More |
| 41 | than one line may be passed at a time. |
| 42 | |
| 43 | |
| 44 | .. function:: b2a_base64(data) |
| 45 | |
| 46 | Convert binary data to a line of ASCII characters in base64 coding. The return |
| 47 | value is the converted line, including a newline char. The length of *data* |
| 48 | should be at most 57 to adhere to the base64 standard. |
| 49 | |
| 50 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 51 | .. function:: a2b_qp(string, header=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | Convert a block of quoted-printable data back to binary and return the binary |
| 54 | data. More than one line may be passed at a time. If the optional argument |
| 55 | *header* is present and true, underscores will be decoded as spaces. |
| 56 | |
| 57 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 58 | .. function:: b2a_qp(data, quotetabs=False, istext=True, header=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | Convert binary data to a line(s) of ASCII characters in quoted-printable |
| 61 | encoding. The return value is the converted line(s). If the optional argument |
| 62 | *quotetabs* is present and true, all tabs and spaces will be encoded. If the |
| 63 | optional argument *istext* is present and true, newlines are not encoded but |
| 64 | trailing whitespace will be encoded. If the optional argument *header* is |
| 65 | present and true, spaces will be encoded as underscores per RFC1522. If the |
| 66 | optional argument *header* is present and false, newline characters will be |
| 67 | encoded as well; otherwise linefeed conversion might corrupt the binary data |
| 68 | stream. |
| 69 | |
| 70 | |
| 71 | .. function:: a2b_hqx(string) |
| 72 | |
| 73 | Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression. |
| 74 | The string should contain a complete number of binary bytes, or (in case of the |
| 75 | last portion of the binhex4 data) have the remaining bits zero. |
| 76 | |
| 77 | |
| 78 | .. function:: rledecode_hqx(data) |
| 79 | |
| 80 | Perform RLE-decompression on the data, as per the binhex4 standard. The |
| 81 | algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count. |
| 82 | A count of ``0`` specifies a byte value of ``0x90``. The routine returns the |
| 83 | decompressed data, unless data input data ends in an orphaned repeat indicator, |
| 84 | in which case the :exc:`Incomplete` exception is raised. |
| 85 | |
| 86 | |
| 87 | .. function:: rlecode_hqx(data) |
| 88 | |
| 89 | Perform binhex4 style RLE-compression on *data* and return the result. |
| 90 | |
| 91 | |
| 92 | .. function:: b2a_hqx(data) |
| 93 | |
| 94 | Perform hexbin4 binary-to-ASCII translation and return the resulting string. The |
| 95 | argument should already be RLE-coded, and have a length divisible by 3 (except |
| 96 | possibly the last fragment). |
| 97 | |
| 98 | |
| 99 | .. function:: crc_hqx(data, crc) |
| 100 | |
| 101 | Compute the binhex4 crc value of *data*, starting with an initial *crc* and |
| 102 | returning the result. |
| 103 | |
| 104 | |
| 105 | .. function:: crc32(data[, crc]) |
| 106 | |
| 107 | Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This |
| 108 | is consistent with the ZIP file checksum. Since the algorithm is designed for |
| 109 | use as a checksum algorithm, it is not suitable for use as a general hash |
| 110 | algorithm. Use as follows:: |
| 111 | |
Georg Brandl | ede6c2a | 2010-01-05 10:22:04 +0000 | [diff] [blame] | 112 | print(binascii.crc32(b"hello world")) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | # Or, in two pieces: |
Georg Brandl | ede6c2a | 2010-01-05 10:22:04 +0000 | [diff] [blame] | 114 | crc = binascii.crc32(b"hello") |
| 115 | crc = binascii.crc32(b" world", crc) & 0xffffffff |
| 116 | print('crc32 = {:#010x}'.format(crc)) |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 117 | |
| 118 | .. note:: |
| 119 | To generate the same numeric value across all Python versions and |
| 120 | platforms use crc32(data) & 0xffffffff. If you are only using |
| 121 | the checksum in packed binary format this is not necessary as the |
Gregory P. Smith | fa6cf39 | 2009-02-01 00:30:50 +0000 | [diff] [blame] | 122 | return value is the correct 32bit binary representation |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 123 | regardless of sign. |
| 124 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 126 | .. function:: b2a_hex(data) |
| 127 | hexlify(data) |
| 128 | |
| 129 | Return the hexadecimal representation of the binary *data*. Every byte of |
| 130 | *data* is converted into the corresponding 2-digit hex representation. The |
| 131 | resulting string is therefore twice as long as the length of *data*. |
| 132 | |
| 133 | |
| 134 | .. function:: a2b_hex(hexstr) |
| 135 | unhexlify(hexstr) |
| 136 | |
| 137 | Return the binary data represented by the hexadecimal string *hexstr*. This |
| 138 | function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number |
| 139 | of hexadecimal digits (which can be upper or lower case), otherwise a |
| 140 | :exc:`TypeError` is raised. |
| 141 | |
| 142 | |
| 143 | .. exception:: Error |
| 144 | |
| 145 | Exception raised on errors. These are usually programming errors. |
| 146 | |
| 147 | |
| 148 | .. exception:: Incomplete |
| 149 | |
| 150 | Exception raised on incomplete data. These are usually not programming errors, |
| 151 | but may be handled by reading a little more data and trying again. |
| 152 | |
| 153 | |
| 154 | .. seealso:: |
| 155 | |
| 156 | Module :mod:`base64` |
| 157 | Support for base64 encoding used in MIME email messages. |
| 158 | |
| 159 | Module :mod:`binhex` |
| 160 | Support for the binhex format used on the Macintosh. |
| 161 | |
| 162 | Module :mod:`uu` |
| 163 | Support for UU encoding used on Unix. |
| 164 | |
| 165 | Module :mod:`quopri` |
| 166 | Support for quoted-printable encoding used in MIME email messages. |
| 167 | |