| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-binhex.html" /> |
| 13 | <link rel="prev" href="module-base64.html" /> |
| 14 | <link rel="parent" href="netdata.html" /> |
| 15 | <link rel="next" href="module-binhex.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>12.13 binascii -- Convert between binary and ASCII</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="12.12 base64 " |
| 25 | href="module-base64.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" |
| 28 | href="netdata.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="12.14 binhex " |
| 31 | href="module-binhex.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-base64.html">12.12 base64 </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-binhex.html">12.14 binhex </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION00141300000000000000000"> |
| 56 | 12.13 <tt class="module">binascii</tt> -- |
| 57 | Convert between binary and ASCII</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-binascii"></A> |
| 62 | |
| 63 | <P> |
| 64 | The <tt class="module">binascii</tt> module contains a number of methods to convert |
| 65 | between binary and various ASCII-encoded binary |
| 66 | representations. Normally, you will not use these functions directly |
| 67 | but use wrapper modules like <tt class="module"><a href="module-uu.html">uu</a></tt><a id='l2h-4143' xml:id='l2h-4143'></a> or |
| 68 | <tt class="module"><a href="module-binhex.html">binhex</a></tt><a id='l2h-4144' xml:id='l2h-4144'></a> instead, this module solely |
| 69 | exists because bit-manipulation of large amounts of data is slow in |
| 70 | Python. |
| 71 | |
| 72 | <P> |
| 73 | The <tt class="module">binascii</tt> module defines the following functions: |
| 74 | |
| 75 | <P> |
| 76 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 77 | <td><nobr><b><tt id='l2h-4125' xml:id='l2h-4125' class="function">a2b_uu</tt></b>(</nobr></td> |
| 78 | <td><var>string</var>)</td></tr></table></dt> |
| 79 | <dd> |
| 80 | Convert a single line of uuencoded data back to binary and return the |
| 81 | binary data. Lines normally contain 45 (binary) bytes, except for the |
| 82 | last line. Line data may be followed by whitespace. |
| 83 | </dl> |
| 84 | |
| 85 | <P> |
| 86 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 87 | <td><nobr><b><tt id='l2h-4126' xml:id='l2h-4126' class="function">b2a_uu</tt></b>(</nobr></td> |
| 88 | <td><var>data</var>)</td></tr></table></dt> |
| 89 | <dd> |
| 90 | Convert binary data to a line of ASCII characters, the return value |
| 91 | is the converted line, including a newline char. The length of |
| 92 | <var>data</var> should be at most 45. |
| 93 | </dl> |
| 94 | |
| 95 | <P> |
| 96 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 97 | <td><nobr><b><tt id='l2h-4127' xml:id='l2h-4127' class="function">a2b_base64</tt></b>(</nobr></td> |
| 98 | <td><var>string</var>)</td></tr></table></dt> |
| 99 | <dd> |
| 100 | Convert a block of base64 data back to binary and return the |
| 101 | binary data. More than one line may be passed at a time. |
| 102 | </dl> |
| 103 | |
| 104 | <P> |
| 105 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 106 | <td><nobr><b><tt id='l2h-4128' xml:id='l2h-4128' class="function">b2a_base64</tt></b>(</nobr></td> |
| 107 | <td><var>data</var>)</td></tr></table></dt> |
| 108 | <dd> |
| 109 | Convert binary data to a line of ASCII characters in base64 coding. |
| 110 | The return value is the converted line, including a newline char. |
| 111 | The length of <var>data</var> should be at most 57 to adhere to the base64 |
| 112 | standard. |
| 113 | </dl> |
| 114 | |
| 115 | <P> |
| 116 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 117 | <td><nobr><b><tt id='l2h-4129' xml:id='l2h-4129' class="function">a2b_qp</tt></b>(</nobr></td> |
| 118 | <td><var>string</var><big>[</big><var>, header</var><big>]</big><var></var>)</td></tr></table></dt> |
| 119 | <dd> |
| 120 | Convert a block of quoted-printable data back to binary and return the |
| 121 | binary data. More than one line may be passed at a time. |
| 122 | If the optional argument <var>header</var> is present and true, underscores |
| 123 | will be decoded as spaces. |
| 124 | </dl> |
| 125 | |
| 126 | <P> |
| 127 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 128 | <td><nobr><b><tt id='l2h-4130' xml:id='l2h-4130' class="function">b2a_qp</tt></b>(</nobr></td> |
| 129 | <td><var>data</var><big>[</big><var>, quotetabs, istext, header</var><big>]</big><var></var>)</td></tr></table></dt> |
| 130 | <dd> |
| 131 | Convert binary data to a line(s) of ASCII characters in |
| 132 | quoted-printable encoding. The return value is the converted line(s). |
| 133 | If the optional argument <var>quotetabs</var> is present and true, all tabs |
| 134 | and spaces will be encoded. |
| 135 | If the optional argument <var>istext</var> is present and true, |
| 136 | newlines are not encoded but trailing whitespace will be encoded. |
| 137 | If the optional argument <var>header</var> is |
| 138 | present and true, spaces will be encoded as underscores per RFC1522. |
| 139 | If the optional argument <var>header</var> is present and false, newline |
| 140 | characters will be encoded as well; otherwise linefeed conversion might |
| 141 | corrupt the binary data stream. |
| 142 | </dl> |
| 143 | |
| 144 | <P> |
| 145 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 146 | <td><nobr><b><tt id='l2h-4131' xml:id='l2h-4131' class="function">a2b_hqx</tt></b>(</nobr></td> |
| 147 | <td><var>string</var>)</td></tr></table></dt> |
| 148 | <dd> |
| 149 | Convert binhex4 formatted ASCII data to binary, without doing |
| 150 | RLE-decompression. The string should contain a complete number of |
| 151 | binary bytes, or (in case of the last portion of the binhex4 data) |
| 152 | have the remaining bits zero. |
| 153 | </dl> |
| 154 | |
| 155 | <P> |
| 156 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 157 | <td><nobr><b><tt id='l2h-4132' xml:id='l2h-4132' class="function">rledecode_hqx</tt></b>(</nobr></td> |
| 158 | <td><var>data</var>)</td></tr></table></dt> |
| 159 | <dd> |
| 160 | Perform RLE-decompression on the data, as per the binhex4 |
| 161 | standard. The algorithm uses <code>0x90</code> after a byte as a repeat |
| 162 | indicator, followed by a count. A count of <code>0</code> specifies a byte |
| 163 | value of <code>0x90</code>. The routine returns the decompressed data, |
| 164 | unless data input data ends in an orphaned repeat indicator, in which |
| 165 | case the <tt class="exception">Incomplete</tt> exception is raised. |
| 166 | </dl> |
| 167 | |
| 168 | <P> |
| 169 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 170 | <td><nobr><b><tt id='l2h-4133' xml:id='l2h-4133' class="function">rlecode_hqx</tt></b>(</nobr></td> |
| 171 | <td><var>data</var>)</td></tr></table></dt> |
| 172 | <dd> |
| 173 | Perform binhex4 style RLE-compression on <var>data</var> and return the |
| 174 | result. |
| 175 | </dl> |
| 176 | |
| 177 | <P> |
| 178 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 179 | <td><nobr><b><tt id='l2h-4134' xml:id='l2h-4134' class="function">b2a_hqx</tt></b>(</nobr></td> |
| 180 | <td><var>data</var>)</td></tr></table></dt> |
| 181 | <dd> |
| 182 | Perform hexbin4 binary-to-ASCII translation and return the |
| 183 | resulting string. The argument should already be RLE-coded, and have a |
| 184 | length divisible by 3 (except possibly the last fragment). |
| 185 | </dl> |
| 186 | |
| 187 | <P> |
| 188 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 189 | <td><nobr><b><tt id='l2h-4135' xml:id='l2h-4135' class="function">crc_hqx</tt></b>(</nobr></td> |
| 190 | <td><var>data, crc</var>)</td></tr></table></dt> |
| 191 | <dd> |
| 192 | Compute the binhex4 crc value of <var>data</var>, starting with an initial |
| 193 | <var>crc</var> and returning the result. |
| 194 | </dl> |
| 195 | |
| 196 | <P> |
| 197 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 198 | <td><nobr><b><tt id='l2h-4136' xml:id='l2h-4136' class="function">crc32</tt></b>(</nobr></td> |
| 199 | <td><var>data</var><big>[</big><var>, crc</var><big>]</big><var></var>)</td></tr></table></dt> |
| 200 | <dd> |
| 201 | Compute CRC-32, the 32-bit checksum of data, starting with an initial |
| 202 | crc. This is consistent with the ZIP file checksum. Since the |
| 203 | algorithm is designed for use as a checksum algorithm, it is not |
| 204 | suitable for use as a general hash algorithm. Use as follows: |
| 205 | <div class="verbatim"><pre> |
| 206 | print binascii.crc32("hello world") |
| 207 | # Or, in two pieces: |
| 208 | crc = binascii.crc32("hello") |
| 209 | crc = binascii.crc32(" world", crc) |
| 210 | print crc |
| 211 | </pre></div> |
| 212 | </dl> |
| 213 | |
| 214 | <P> |
| 215 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 216 | <td><nobr><b><tt id='l2h-4137' xml:id='l2h-4137' class="function">b2a_hex</tt></b>(</nobr></td> |
| 217 | <td><var>data</var>)</td></tr></table></dt> |
| 218 | <dd> |
| 219 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 220 | <td><nobr><b><tt class="function">hexlify</tt></b>(</nobr></td> |
| 221 | <td><var>data</var>)</td></tr></table></dt> |
| 222 | <dd>Return the hexadecimal representation of the binary <var>data</var>. Every |
| 223 | byte of <var>data</var> is converted into the corresponding 2-digit hex |
| 224 | representation. The resulting string is therefore twice as long as |
| 225 | the length of <var>data</var>. |
| 226 | </dl> |
| 227 | |
| 228 | <P> |
| 229 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 230 | <td><nobr><b><tt id='l2h-4139' xml:id='l2h-4139' class="function">a2b_hex</tt></b>(</nobr></td> |
| 231 | <td><var>hexstr</var>)</td></tr></table></dt> |
| 232 | <dd> |
| 233 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 234 | <td><nobr><b><tt class="function">unhexlify</tt></b>(</nobr></td> |
| 235 | <td><var>hexstr</var>)</td></tr></table></dt> |
| 236 | <dd>Return the binary data represented by the hexadecimal string |
| 237 | <var>hexstr</var>. This function is the inverse of <tt class="function">b2a_hex()</tt>. |
| 238 | <var>hexstr</var> must contain an even number of hexadecimal digits (which |
| 239 | can be upper or lower case), otherwise a <tt class="exception">TypeError</tt> is |
| 240 | raised. |
| 241 | </dl> |
| 242 | |
| 243 | <P> |
| 244 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-4141' xml:id='l2h-4141' class="exception">Error</tt></b></dt> |
| 245 | <dd> |
| 246 | Exception raised on errors. These are usually programming errors. |
| 247 | </dd></dl> |
| 248 | |
| 249 | <P> |
| 250 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-4142' xml:id='l2h-4142' class="exception">Incomplete</tt></b></dt> |
| 251 | <dd> |
| 252 | Exception raised on incomplete data. These are usually not programming |
| 253 | errors, but may be handled by reading a little more data and trying |
| 254 | again. |
| 255 | </dd></dl> |
| 256 | |
| 257 | <P> |
| 258 | <div class="seealso"> |
| 259 | <p class="heading">See Also:</p> |
| 260 | |
| 261 | <dl compact="compact" class="seemodule"> |
| 262 | <dt>Module <b><tt class="module"><a href="module-base64.html">base64</a></tt>:</b> |
| 263 | <dd>Support for base64 encoding used in MIME email messages. |
| 264 | </dl> |
| 265 | |
| 266 | <P> |
| 267 | <dl compact="compact" class="seemodule"> |
| 268 | <dt>Module <b><tt class="module"><a href="module-binhex.html">binhex</a></tt>:</b> |
| 269 | <dd>Support for the binhex format used on the Macintosh. |
| 270 | </dl> |
| 271 | |
| 272 | <P> |
| 273 | <dl compact="compact" class="seemodule"> |
| 274 | <dt>Module <b><tt class="module"><a href="module-uu.html">uu</a></tt>:</b> |
| 275 | <dd>Support for UU encoding used on <span class="Unix">Unix</span>. |
| 276 | </dl> |
| 277 | |
| 278 | <P> |
| 279 | <dl compact="compact" class="seemodule"> |
| 280 | <dt>Module <b><tt class="module"><a href="module-quopri.html">quopri</a></tt>:</b> |
| 281 | <dd>Support for quoted-printable encoding used in MIME email messages. . |
| 282 | </dl> |
| 283 | </div> |
| 284 | |
| 285 | <DIV CLASS="navigation"> |
| 286 | <div class='online-navigation'> |
| 287 | <p></p><hr /> |
| 288 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 289 | <tr> |
| 290 | <td class='online-navigation'><a rel="prev" title="12.12 base64 " |
| 291 | href="module-base64.html"><img src='../icons/previous.png' |
| 292 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 293 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" |
| 294 | href="netdata.html"><img src='../icons/up.png' |
| 295 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 296 | <td class='online-navigation'><a rel="next" title="12.14 binhex " |
| 297 | href="module-binhex.html"><img src='../icons/next.png' |
| 298 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 299 | <td align="center" width="100%">Python Library Reference</td> |
| 300 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 301 | href="contents.html"><img src='../icons/contents.png' |
| 302 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 303 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 304 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 305 | <td class='online-navigation'><a rel="index" title="Index" |
| 306 | href="genindex.html"><img src='../icons/index.png' |
| 307 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 308 | </tr></table> |
| 309 | <div class='online-navigation'> |
| 310 | <b class="navlabel">Previous:</b> |
| 311 | <a class="sectref" rel="prev" href="module-base64.html">12.12 base64 </A> |
| 312 | <b class="navlabel">Up:</b> |
| 313 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> |
| 314 | <b class="navlabel">Next:</b> |
| 315 | <a class="sectref" rel="next" href="module-binhex.html">12.14 binhex </A> |
| 316 | </div> |
| 317 | </div> |
| 318 | <hr /> |
| 319 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 320 | </DIV> |
| 321 | <!--End of Navigation Panel--> |
| 322 | <ADDRESS> |
| 323 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 324 | </ADDRESS> |
| 325 | </BODY> |
| 326 | </HTML> |