This commit was manufactured by cvs2svn to create tag 'r241c1'.
[python/dscho.git] / Doc / lib / libbinascii.tex
blob9850418feff7fdba431887c7c85d029684e06a36
1 \section{\module{binascii} ---
2 Convert between binary and \ASCII}
4 \declaremodule{builtin}{binascii}
5 \modulesynopsis{Tools for converting between binary and various
6 \ASCII-encoded binary representations.}
9 The \module{binascii} module contains a number of methods to convert
10 between binary and various \ASCII-encoded binary
11 representations. Normally, you will not use these functions directly
12 but use wrapper modules like \refmodule{uu}\refstmodindex{uu} or
13 \refmodule{binhex}\refstmodindex{binhex} instead, this module solely
14 exists because bit-manipulation of large amounts of data is slow in
15 Python.
17 The \module{binascii} module defines the following functions:
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}
25 \begin{funcdesc}{b2a_uu}{data}
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.
29 \end{funcdesc}
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}
36 \begin{funcdesc}{b2a_base64}{data}
37 Convert binary data to a line of \ASCII{} characters in base64 coding.
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}
43 \begin{funcdesc}{a2b_qp}{string\optional{, header}}
44 Convert a block of quoted-printable data back to binary and return the
45 binary data. More than one line may be passed at a time.
46 If the optional argument \var{header} is present and true, underscores
47 will be decoded as spaces.
48 \end{funcdesc}
50 \begin{funcdesc}{b2a_qp}{data\optional{, quotetabs, istext, header}}
51 Convert binary data to a line(s) of \ASCII{} characters in
52 quoted-printable encoding. The return value is the converted line(s).
53 If the optional argument \var{quotetabs} is present and true, all tabs
54 and spaces will be encoded. If the optional argument \var{header} is
55 present and true, spaces will be encoded as underscores per RFC1522.
56 If the optional argument \var{header} is present and false, newline
57 characters will be encoded as well, otherwise linefeed conversion might
58 corrupt the binary data stream.
59 \end{funcdesc}
61 \begin{funcdesc}{a2b_hqx}{string}
62 Convert binhex4 formatted \ASCII{} data to binary, without doing
63 RLE-decompression. The string should contain a complete number of
64 binary bytes, or (in case of the last portion of the binhex4 data)
65 have the remaining bits zero.
66 \end{funcdesc}
68 \begin{funcdesc}{rledecode_hqx}{data}
69 Perform RLE-decompression on the data, as per the binhex4
70 standard. The algorithm uses \code{0x90} after a byte as a repeat
71 indicator, followed by a count. A count of \code{0} specifies a byte
72 value of \code{0x90}. The routine returns the decompressed data,
73 unless data input data ends in an orphaned repeat indicator, in which
74 case the \exception{Incomplete} exception is raised.
75 \end{funcdesc}
77 \begin{funcdesc}{rlecode_hqx}{data}
78 Perform binhex4 style RLE-compression on \var{data} and return the
79 result.
80 \end{funcdesc}
82 \begin{funcdesc}{b2a_hqx}{data}
83 Perform hexbin4 binary-to-\ASCII{} translation and return the
84 resulting string. The argument should already be RLE-coded, and have a
85 length divisible by 3 (except possibly the last fragment).
86 \end{funcdesc}
88 \begin{funcdesc}{crc_hqx}{data, crc}
89 Compute the binhex4 crc value of \var{data}, starting with an initial
90 \var{crc} and returning the result.
91 \end{funcdesc}
93 \begin{funcdesc}{crc32}{data\optional{, crc}}
94 Compute CRC-32, the 32-bit checksum of data, starting with an initial
95 crc. This is consistent with the ZIP file checksum. Since the
96 algorithm is designed for use as a checksum algorithm, it is not
97 suitable for use as a general hash algorithm. Use as follows:
98 \begin{verbatim}
99 print binascii.crc32("hello world")
100 # Or, in two pieces:
101 crc = binascii.crc32("hello")
102 crc = binascii.crc32(" world", crc)
103 print crc
104 \end{verbatim}
105 \end{funcdesc}
107 \begin{funcdesc}{b2a_hex}{data}
108 \funcline{hexlify}{data}
109 Return the hexadecimal representation of the binary \var{data}. Every
110 byte of \var{data} is converted into the corresponding 2-digit hex
111 representation. The resulting string is therefore twice as long as
112 the length of \var{data}.
113 \end{funcdesc}
115 \begin{funcdesc}{a2b_hex}{hexstr}
116 \funcline{unhexlify}{hexstr}
117 Return the binary data represented by the hexadecimal string
118 \var{hexstr}. This function is the inverse of \function{b2a_hex()}.
119 \var{hexstr} must contain an even number of hexadecimal digits (which
120 can be upper or lower case), otherwise a \exception{TypeError} is
121 raised.
122 \end{funcdesc}
124 \begin{excdesc}{Error}
125 Exception raised on errors. These are usually programming errors.
126 \end{excdesc}
128 \begin{excdesc}{Incomplete}
129 Exception raised on incomplete data. These are usually not programming
130 errors, but may be handled by reading a little more data and trying
131 again.
132 \end{excdesc}
135 \begin{seealso}
136 \seemodule{base64}{Support for base64 encoding used in MIME email messages.}
138 \seemodule{binhex}{Support for the binhex format used on the Macintosh.}
140 \seemodule{uu}{Support for UU encoding used on \UNIX.}
142 \seemodule{quopri}{Support for quoted-printable encoding used in MIME email messages. }
143 \end{seealso}