Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-binascii.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-binhex.html" />
<link rel="prev" href="module-base64.html" />
<link rel="parent" href="netdata.html" />
<link rel="next" href="module-binhex.html" />
<meta name='aesop' content='information' />
<title>12.13 binascii -- Convert between binary and ASCII</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.12 base64 "
href="module-base64.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="12. Internet Data Handling"
href="netdata.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="12.14 binhex "
href="module-binhex.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-base64.html">12.12 base64 </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-binhex.html">12.14 binhex </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION00141300000000000000000">
12.13 <tt class="module">binascii</tt> --
Convert between binary and ASCII</A>
</H1>
<P>
<A NAME="module-binascii"></A>
<P>
The <tt class="module">binascii</tt> module contains a number of methods to convert
between binary and various ASCII-encoded binary
representations. Normally, you will not use these functions directly
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
<tt class="module"><a href="module-binhex.html">binhex</a></tt><a id='l2h-4144' xml:id='l2h-4144'></a> instead, this module solely
exists because bit-manipulation of large amounts of data is slow in
Python.
<P>
The <tt class="module">binascii</tt> module defines the following functions:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4125' xml:id='l2h-4125' class="function">a2b_uu</tt></b>(</nobr></td>
<td><var>string</var>)</td></tr></table></dt>
<dd>
Convert a single line of uuencoded data back to binary and return the
binary data. Lines normally contain 45 (binary) bytes, except for the
last line. Line data may be followed by whitespace.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4126' xml:id='l2h-4126' class="function">b2a_uu</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Convert binary data to a line of ASCII characters, the return value
is the converted line, including a newline char. The length of
<var>data</var> should be at most 45.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4127' xml:id='l2h-4127' class="function">a2b_base64</tt></b>(</nobr></td>
<td><var>string</var>)</td></tr></table></dt>
<dd>
Convert a block of base64 data back to binary and return the
binary data. More than one line may be passed at a time.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4128' xml:id='l2h-4128' class="function">b2a_base64</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Convert binary data to a line of ASCII characters in base64 coding.
The return value is the converted line, including a newline char.
The length of <var>data</var> should be at most 57 to adhere to the base64
standard.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4129' xml:id='l2h-4129' class="function">a2b_qp</tt></b>(</nobr></td>
<td><var>string</var><big>[</big><var>, header</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Convert a block of quoted-printable data back to binary and return the
binary data. More than one line may be passed at a time.
If the optional argument <var>header</var> is present and true, underscores
will be decoded as spaces.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4130' xml:id='l2h-4130' class="function">b2a_qp</tt></b>(</nobr></td>
<td><var>data</var><big>[</big><var>, quotetabs, istext, header</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Convert binary data to a line(s) of ASCII characters in
quoted-printable encoding. The return value is the converted line(s).
If the optional argument <var>quotetabs</var> is present and true, all tabs
and spaces will be encoded.
If the optional argument <var>istext</var> is present and true,
newlines are not encoded but trailing whitespace will be encoded.
If the optional argument <var>header</var> is
present and true, spaces will be encoded as underscores per RFC1522.
If the optional argument <var>header</var> is present and false, newline
characters will be encoded as well; otherwise linefeed conversion might
corrupt the binary data stream.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4131' xml:id='l2h-4131' class="function">a2b_hqx</tt></b>(</nobr></td>
<td><var>string</var>)</td></tr></table></dt>
<dd>
Convert binhex4 formatted ASCII data to binary, without doing
RLE-decompression. The string should contain a complete number of
binary bytes, or (in case of the last portion of the binhex4 data)
have the remaining bits zero.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4132' xml:id='l2h-4132' class="function">rledecode_hqx</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Perform RLE-decompression on the data, as per the binhex4
standard. The algorithm uses <code>0x90</code> after a byte as a repeat
indicator, followed by a count. A count of <code>0</code> specifies a byte
value of <code>0x90</code>. The routine returns the decompressed data,
unless data input data ends in an orphaned repeat indicator, in which
case the <tt class="exception">Incomplete</tt> exception is raised.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4133' xml:id='l2h-4133' class="function">rlecode_hqx</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Perform binhex4 style RLE-compression on <var>data</var> and return the
result.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4134' xml:id='l2h-4134' class="function">b2a_hqx</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Perform hexbin4 binary-to-ASCII translation and return the
resulting string. The argument should already be RLE-coded, and have a
length divisible by 3 (except possibly the last fragment).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4135' xml:id='l2h-4135' class="function">crc_hqx</tt></b>(</nobr></td>
<td><var>data, crc</var>)</td></tr></table></dt>
<dd>
Compute the binhex4 crc value of <var>data</var>, starting with an initial
<var>crc</var> and returning the result.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4136' xml:id='l2h-4136' class="function">crc32</tt></b>(</nobr></td>
<td><var>data</var><big>[</big><var>, crc</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compute CRC-32, the 32-bit checksum of data, starting with an initial
crc. This is consistent with the ZIP file checksum. Since the
algorithm is designed for use as a checksum algorithm, it is not
suitable for use as a general hash algorithm. Use as follows:
<div class="verbatim"><pre>
print binascii.crc32("hello world")
# Or, in two pieces:
crc = binascii.crc32("hello")
crc = binascii.crc32(" world", crc)
print crc
</pre></div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4137' xml:id='l2h-4137' class="function">b2a_hex</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
<dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt class="function">hexlify</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>Return the hexadecimal representation of the binary <var>data</var>. Every
byte of <var>data</var> is converted into the corresponding 2-digit hex
representation. The resulting string is therefore twice as long as
the length of <var>data</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4139' xml:id='l2h-4139' class="function">a2b_hex</tt></b>(</nobr></td>
<td><var>hexstr</var>)</td></tr></table></dt>
<dd>
<dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt class="function">unhexlify</tt></b>(</nobr></td>
<td><var>hexstr</var>)</td></tr></table></dt>
<dd>Return the binary data represented by the hexadecimal string
<var>hexstr</var>. This function is the inverse of <tt class="function">b2a_hex()</tt>.
<var>hexstr</var> must contain an even number of hexadecimal digits (which
can be upper or lower case), otherwise a <tt class="exception">TypeError</tt> is
raised.
</dl>
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4141' xml:id='l2h-4141' class="exception">Error</tt></b></dt>
<dd>
Exception raised on errors. These are usually programming errors.
</dd></dl>
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4142' xml:id='l2h-4142' class="exception">Incomplete</tt></b></dt>
<dd>
Exception raised on incomplete data. These are usually not programming
errors, but may be handled by reading a little more data and trying
again.
</dd></dl>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-base64.html">base64</a></tt>:</b>
<dd>Support for base64 encoding used in MIME email messages.
</dl>
<P>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-binhex.html">binhex</a></tt>:</b>
<dd>Support for the binhex format used on the Macintosh.
</dl>
<P>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-uu.html">uu</a></tt>:</b>
<dd>Support for UU encoding used on <span class="Unix">Unix</span>.
</dl>
<P>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-quopri.html">quopri</a></tt>:</b>
<dd>Support for quoted-printable encoding used in MIME email messages. .
</dl>
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="12.12 base64 "
href="module-base64.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="12. Internet Data Handling"
href="netdata.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="12.14 binhex "
href="module-binhex.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-base64.html">12.12 base64 </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-binhex.html">12.14 binhex </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>