Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{binascii} --- |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 2 | Convert between binary and \ASCII{}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{binascii} |
Fred Drake | 2d62395 | 1998-08-07 16:00:30 +0000 | [diff] [blame] | 5 | \modulesynopsis{Tools for converting between binary and various |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 6 | \ASCII{}-encoded binary representations.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 8 | |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 9 | The \module{binascii} module contains a number of methods to convert |
| 10 | between binary and various \ASCII{}-encoded binary |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 11 | representations. Normally, you will not use these functions directly |
| 12 | but use wrapper modules like \refmodule{uu}\refstmodindex{uu} or |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 13 | \refmodule{binhex}\refstmodindex{binhex} instead, this module solely |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 14 | exists because bit-manipulation of large amounts of data is slow in |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 15 | Python. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 16 | |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 17 | The \module{binascii} module defines the following functions: |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 18 | |
| 19 | \begin{funcdesc}{a2b_uu}{string} |
| 20 | Convert a single line of uuencoded data back to binary and return the |
| 21 | binary data. Lines normally contain 45 (binary) bytes, except for the |
| 22 | last line. Line data may be followed by whitespace. |
| 23 | \end{funcdesc} |
| 24 | |
| 25 | \begin{funcdesc}{b2a_uu}{data} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 26 | Convert binary data to a line of \ASCII{} characters, the return value |
| 27 | is the converted line, including a newline char. The length of |
| 28 | \var{data} should be at most 45. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 29 | \end{funcdesc} |
| 30 | |
Jack Jansen | 06cf5d0 | 1995-10-10 14:41:03 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{a2b_base64}{string} |
| 32 | Convert a block of base64 data back to binary and return the |
| 33 | binary data. More than one line may be passed at a time. |
| 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{b2a_base64}{data} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 37 | Convert binary data to a line of \ASCII{} characters in base64 coding. |
Jack Jansen | 06cf5d0 | 1995-10-10 14:41:03 +0000 | [diff] [blame] | 38 | The return value is the converted line, including a newline char. |
| 39 | The length of \var{data} should be at most 57 to adhere to the base64 |
| 40 | standard. |
| 41 | \end{funcdesc} |
| 42 | |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 43 | \begin{funcdesc}{a2b_hqx}{string} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 44 | Convert binhex4 formatted \ASCII{} data to binary, without doing |
| 45 | RLE-decompression. The string should contain a complete number of |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 46 | binary bytes, or (in case of the last portion of the binhex4 data) |
| 47 | have the remaining bits zero. |
| 48 | \end{funcdesc} |
| 49 | |
| 50 | \begin{funcdesc}{rledecode_hqx}{data} |
| 51 | Perform RLE-decompression on the data, as per the binhex4 |
| 52 | standard. The algorithm uses \code{0x90} after a byte as a repeat |
| 53 | indicator, followed by a count. A count of \code{0} specifies a byte |
| 54 | value of \code{0x90}. The routine returns the decompressed data, |
| 55 | unless data input data ends in an orphaned repeat indicator, in which |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 56 | case the \exception{Incomplete} exception is raised. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{rlecode_hqx}{data} |
| 60 | Perform binhex4 style RLE-compression on \var{data} and return the |
| 61 | result. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{funcdesc}{b2a_hqx}{data} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 65 | Perform hexbin4 binary-to-\ASCII{} translation and return the |
| 66 | resulting string. The argument should already be RLE-coded, and have a |
| 67 | length divisible by 3 (except possibly the last fragment). |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 68 | \end{funcdesc} |
| 69 | |
| 70 | \begin{funcdesc}{crc_hqx}{data, crc} |
| 71 | Compute the binhex4 crc value of \var{data}, starting with an initial |
| 72 | \var{crc} and returning the result. |
| 73 | \end{funcdesc} |
Guido van Rossum | 8006d31 | 2000-02-16 21:13:37 +0000 | [diff] [blame] | 74 | |
| 75 | \begin{funcdesc}{crc32}{data\optional{, crc}} |
| 76 | Compute CRC-32, the 32-bit checksum of data, starting with an initial |
| 77 | crc. This is consistent with the ZIP file checksum. Use as follows: |
| 78 | \begin{verbatim} |
| 79 | print binascii.crc32("hello world") |
| 80 | # Or, in two pieces: |
| 81 | crc = binascii.crc32("hello") |
| 82 | crc = binascii.crc32(" world", crc) |
| 83 | print crc |
| 84 | \end{verbatim} |
| 85 | \end{funcdesc} |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 87 | \begin{funcdesc}{b2a_hex}{data} |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 88 | \funcline{hexlify}{data} |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 89 | Return the hexadecimal representation of the binary \var{data}. Every |
| 90 | byte of \var{data} is converted into the corresponding 2-digit hex |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 91 | representation. The resulting string is therefore twice as long as |
| 92 | the length of \var{data}. |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 93 | \end{funcdesc} |
| 94 | |
| 95 | \begin{funcdesc}{a2b_hex}{hexstr} |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 96 | \funcline{unhexlify}{hexstr} |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 97 | Return the binary data represented by the hexadecimal string |
| 98 | \var{hexstr}. This function is the inverse of \function{b2a_hex()}. |
| 99 | \var{hexstr} must contain an even number of hexadecimal digits (which |
| 100 | can be upper or lower case), otherwise a \exception{TypeError} is |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 101 | raised. |
| 102 | \end{funcdesc} |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 103 | |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 104 | \begin{excdesc}{Error} |
| 105 | Exception raised on errors. These are usually programming errors. |
| 106 | \end{excdesc} |
| 107 | |
| 108 | \begin{excdesc}{Incomplete} |
| 109 | Exception raised on incomplete data. These are usually not programming |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 110 | errors, but may be handled by reading a little more data and trying |
| 111 | again. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 112 | \end{excdesc} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | \begin{seealso} |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 116 | \seemodule{base64}{Support for base64 encoding used in MIME email messages.} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 117 | |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 118 | \seemodule{binhex}{Support for the binhex format used on the Macintosh.} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 119 | |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 120 | \seemodule{uu}{Support for UU encoding used on \UNIX.} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 121 | \end{seealso} |