source: vendor/python/2.5/Doc/lib/libbinascii.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 5.6 KB
Line 
1\section{\module{binascii} ---
2 Convert between binary and \ASCII}
3
4\declaremodule{builtin}{binascii}
5\modulesynopsis{Tools for converting between binary and various
6 \ASCII-encoded binary representations.}
7
8
9The \module{binascii} module contains a number of methods to convert
10between binary and various \ASCII-encoded binary
11representations. Normally, you will not use these functions directly
12but use wrapper modules like \refmodule{uu}\refstmodindex{uu},
13\refmodule{base64}\refstmodindex{base64}, or
14\refmodule{binhex}\refstmodindex{binhex} instead. The \module{binascii} module
15contains low-level functions written in C for greater speed
16that are used by the higher-level modules.
17
18The \module{binascii} module defines the following functions:
19
20\begin{funcdesc}{a2b_uu}{string}
21Convert a single line of uuencoded data back to binary and return the
22binary data. Lines normally contain 45 (binary) bytes, except for the
23last line. Line data may be followed by whitespace.
24\end{funcdesc}
25
26\begin{funcdesc}{b2a_uu}{data}
27Convert binary data to a line of \ASCII{} characters, the return value
28is the converted line, including a newline char. The length of
29\var{data} should be at most 45.
30\end{funcdesc}
31
32\begin{funcdesc}{a2b_base64}{string}
33Convert a block of base64 data back to binary and return the
34binary data. More than one line may be passed at a time.
35\end{funcdesc}
36
37\begin{funcdesc}{b2a_base64}{data}
38Convert binary data to a line of \ASCII{} characters in base64 coding.
39The return value is the converted line, including a newline char.
40The length of \var{data} should be at most 57 to adhere to the base64
41standard.
42\end{funcdesc}
43
44\begin{funcdesc}{a2b_qp}{string\optional{, header}}
45Convert a block of quoted-printable data back to binary and return the
46binary data. More than one line may be passed at a time.
47If the optional argument \var{header} is present and true, underscores
48will be decoded as spaces.
49\end{funcdesc}
50
51\begin{funcdesc}{b2a_qp}{data\optional{, quotetabs, istext, header}}
52Convert binary data to a line(s) of \ASCII{} characters in
53quoted-printable encoding. The return value is the converted line(s).
54If the optional argument \var{quotetabs} is present and true, all tabs
55and spaces will be encoded.
56If the optional argument \var{istext} is present and true,
57newlines are not encoded but trailing whitespace will be encoded.
58If the optional argument \var{header} is
59present and true, spaces will be encoded as underscores per RFC1522.
60If the optional argument \var{header} is present and false, newline
61characters will be encoded as well; otherwise linefeed conversion might
62corrupt the binary data stream.
63\end{funcdesc}
64
65\begin{funcdesc}{a2b_hqx}{string}
66Convert binhex4 formatted \ASCII{} data to binary, without doing
67RLE-decompression. The string should contain a complete number of
68binary bytes, or (in case of the last portion of the binhex4 data)
69have the remaining bits zero.
70\end{funcdesc}
71
72\begin{funcdesc}{rledecode_hqx}{data}
73Perform RLE-decompression on the data, as per the binhex4
74standard. The algorithm uses \code{0x90} after a byte as a repeat
75indicator, followed by a count. A count of \code{0} specifies a byte
76value of \code{0x90}. The routine returns the decompressed data,
77unless data input data ends in an orphaned repeat indicator, in which
78case the \exception{Incomplete} exception is raised.
79\end{funcdesc}
80
81\begin{funcdesc}{rlecode_hqx}{data}
82Perform binhex4 style RLE-compression on \var{data} and return the
83result.
84\end{funcdesc}
85
86\begin{funcdesc}{b2a_hqx}{data}
87Perform hexbin4 binary-to-\ASCII{} translation and return the
88resulting string. The argument should already be RLE-coded, and have a
89length divisible by 3 (except possibly the last fragment).
90\end{funcdesc}
91
92\begin{funcdesc}{crc_hqx}{data, crc}
93Compute the binhex4 crc value of \var{data}, starting with an initial
94\var{crc} and returning the result.
95\end{funcdesc}
96
97\begin{funcdesc}{crc32}{data\optional{, crc}}
98Compute CRC-32, the 32-bit checksum of data, starting with an initial
99crc. This is consistent with the ZIP file checksum. Since the
100algorithm is designed for use as a checksum algorithm, it is not
101suitable for use as a general hash algorithm. Use as follows:
102\begin{verbatim}
103 print binascii.crc32("hello world")
104 # Or, in two pieces:
105 crc = binascii.crc32("hello")
106 crc = binascii.crc32(" world", crc)
107 print crc
108\end{verbatim}
109\end{funcdesc}
110
111\begin{funcdesc}{b2a_hex}{data}
112\funcline{hexlify}{data}
113Return the hexadecimal representation of the binary \var{data}. Every
114byte of \var{data} is converted into the corresponding 2-digit hex
115representation. The resulting string is therefore twice as long as
116the length of \var{data}.
117\end{funcdesc}
118
119\begin{funcdesc}{a2b_hex}{hexstr}
120\funcline{unhexlify}{hexstr}
121Return the binary data represented by the hexadecimal string
122\var{hexstr}. This function is the inverse of \function{b2a_hex()}.
123\var{hexstr} must contain an even number of hexadecimal digits (which
124can be upper or lower case), otherwise a \exception{TypeError} is
125raised.
126\end{funcdesc}
127
128\begin{excdesc}{Error}
129Exception raised on errors. These are usually programming errors.
130\end{excdesc}
131
132\begin{excdesc}{Incomplete}
133Exception raised on incomplete data. These are usually not programming
134errors, but may be handled by reading a little more data and trying
135again.
136\end{excdesc}
137
138
139\begin{seealso}
140 \seemodule{base64}{Support for base64 encoding used in MIME email messages.}
141
142 \seemodule{binhex}{Support for the binhex format used on the Macintosh.}
143
144 \seemodule{uu}{Support for UU encoding used on \UNIX.}
145
146 \seemodule{quopri}{Support for quoted-printable encoding used in MIME email messages. }
147\end{seealso}
Note: See TracBrowser for help on using the repository browser.