blob: 470a7afdf3e6fb441b0efbd6d553aef4d86bfe93 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{binascii} ---
Fred Drakeee4d54e1999-04-23 15:42:36 +00002 Convert between binary and \ASCII{}}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{builtin}{binascii}
Fred Drake2d623951998-08-07 16:00:30 +00005\modulesynopsis{Tools for converting between binary and various
Fred Drakeffbe6871999-04-22 21:23:22 +00006 \ASCII{}-encoded binary representations.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Jack Jansen4549b131995-08-29 11:30:24 +00008
Fred Drake930f1341998-04-03 03:44:56 +00009The \module{binascii} module contains a number of methods to convert
10between binary and various \ASCII{}-encoded binary
Fred Drakeffbe6871999-04-22 21:23:22 +000011representations. Normally, you will not use these functions directly
12but use wrapper modules like \refmodule{uu}\refstmodindex{uu} or
Fred Drakeee4d54e1999-04-23 15:42:36 +000013\refmodule{binhex}\refstmodindex{binhex} instead, this module solely
Fred Drake930f1341998-04-03 03:44:56 +000014exists because bit-manipuation of large amounts of data is slow in
15Python.
Jack Jansen4549b131995-08-29 11:30:24 +000016
Fred Drake930f1341998-04-03 03:44:56 +000017The \module{binascii} module defines the following functions:
Jack Jansen4549b131995-08-29 11:30:24 +000018
19\begin{funcdesc}{a2b_uu}{string}
20Convert a single line of uuencoded data back to binary and return the
21binary data. Lines normally contain 45 (binary) bytes, except for the
22last line. Line data may be followed by whitespace.
23\end{funcdesc}
24
25\begin{funcdesc}{b2a_uu}{data}
Fred Drake930f1341998-04-03 03:44:56 +000026Convert binary data to a line of \ASCII{} characters, the return value
27is the converted line, including a newline char. The length of
28\var{data} should be at most 45.
Jack Jansen4549b131995-08-29 11:30:24 +000029\end{funcdesc}
30
Jack Jansen06cf5d01995-10-10 14:41:03 +000031\begin{funcdesc}{a2b_base64}{string}
32Convert a block of base64 data back to binary and return the
33binary data. More than one line may be passed at a time.
34\end{funcdesc}
35
36\begin{funcdesc}{b2a_base64}{data}
Fred Drake930f1341998-04-03 03:44:56 +000037Convert binary data to a line of \ASCII{} characters in base64 coding.
Jack Jansen06cf5d01995-10-10 14:41:03 +000038The return value is the converted line, including a newline char.
39The length of \var{data} should be at most 57 to adhere to the base64
40standard.
41\end{funcdesc}
42
Jack Jansen4549b131995-08-29 11:30:24 +000043\begin{funcdesc}{a2b_hqx}{string}
Fred Drake930f1341998-04-03 03:44:56 +000044Convert binhex4 formatted \ASCII{} data to binary, without doing
45RLE-decompression. The string should contain a complete number of
Jack Jansen4549b131995-08-29 11:30:24 +000046binary bytes, or (in case of the last portion of the binhex4 data)
47have the remaining bits zero.
48\end{funcdesc}
49
50\begin{funcdesc}{rledecode_hqx}{data}
51Perform RLE-decompression on the data, as per the binhex4
52standard. The algorithm uses \code{0x90} after a byte as a repeat
53indicator, followed by a count. A count of \code{0} specifies a byte
54value of \code{0x90}. The routine returns the decompressed data,
55unless data input data ends in an orphaned repeat indicator, in which
Fred Drake930f1341998-04-03 03:44:56 +000056case the \exception{Incomplete} exception is raised.
Jack Jansen4549b131995-08-29 11:30:24 +000057\end{funcdesc}
58
59\begin{funcdesc}{rlecode_hqx}{data}
60Perform binhex4 style RLE-compression on \var{data} and return the
61result.
62\end{funcdesc}
63
64\begin{funcdesc}{b2a_hqx}{data}
Fred Drake930f1341998-04-03 03:44:56 +000065Perform hexbin4 binary-to-\ASCII{} translation and return the
66resulting string. The argument should already be RLE-coded, and have a
67length divisible by 3 (except possibly the last fragment).
Jack Jansen4549b131995-08-29 11:30:24 +000068\end{funcdesc}
69
70\begin{funcdesc}{crc_hqx}{data, crc}
71Compute the binhex4 crc value of \var{data}, starting with an initial
72\var{crc} and returning the result.
73\end{funcdesc}
74
75\begin{excdesc}{Error}
76Exception raised on errors. These are usually programming errors.
77\end{excdesc}
78
79\begin{excdesc}{Incomplete}
80Exception raised on incomplete data. These are usually not programming
Fred Drake930f1341998-04-03 03:44:56 +000081errors, but may be handled by reading a little more data and trying
82again.
Jack Jansen4549b131995-08-29 11:30:24 +000083\end{excdesc}
Fred Drakeee4d54e1999-04-23 15:42:36 +000084
85
86\begin{seealso}
87 \seemodule{base64}{support for base64 encoding used in MIME email messages}
88
89 \seemodule{binhex}{support for the binhex format used on the Macintosh}
90
91 \seemodule{uu}{support for UU encoding used on \UNIX{}}
92\end{seealso}