498 lines
35 KiB
HTML
498 lines
35 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html lang="en" data-content_root="../">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<meta property="og:title" content="binascii — Convert between binary and ASCII" />
|
|
<meta property="og:type" content="website" />
|
|
<meta property="og:url" content="https://docs.python.org/3/library/binascii.html" />
|
|
<meta property="og:site_name" content="Python documentation" />
|
|
<meta property="og:description" content="The binascii 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 modu..." />
|
|
<meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
|
|
<meta property="og:image:alt" content="Python documentation" />
|
|
<meta name="description" content="The binascii 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 modu..." />
|
|
<meta property="og:image:width" content="200">
|
|
<meta property="og:image:height" content="200">
|
|
<meta name="theme-color" content="#3776ab">
|
|
|
|
<title>binascii — Convert between binary and ASCII — Python 3.13.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
|
|
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=23252803" />
|
|
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
|
|
|
|
<script src="../_static/documentation_options.js?v=5d57ca2d"></script>
|
|
<script src="../_static/doctools.js?v=9bcbadda"></script>
|
|
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
|
|
|
|
<script src="../_static/sidebar.js"></script>
|
|
|
|
<link rel="search" type="application/opensearchdescription+xml"
|
|
title="Search within Python 3.13.3 documentation"
|
|
href="../_static/opensearch.xml"/>
|
|
<link rel="author" title="About these documents" href="../about.html" />
|
|
<link rel="index" title="Index" href="../genindex.html" />
|
|
<link rel="search" title="Search" href="../search.html" />
|
|
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
|
<link rel="next" title="quopri — Encode and decode MIME quoted-printable data" href="quopri.html" />
|
|
<link rel="prev" title="base64 — Base16, Base32, Base64, Base85 Data Encodings" href="base64.html" />
|
|
|
|
<link rel="canonical" href="https://docs.python.org/3/library/binascii.html">
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
|
@media only screen {
|
|
table.full-width-table {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|
|
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
|
|
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|
<script type="text/javascript" src="../_static/menu.js"></script>
|
|
<script type="text/javascript" src="../_static/search-focus.js"></script>
|
|
<script type="text/javascript" src="../_static/themetoggle.js"></script>
|
|
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
|
|
<meta name="readthedocs-addons-api-version" content="1">
|
|
|
|
</head>
|
|
<body>
|
|
<div class="mobile-nav">
|
|
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
|
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
|
<nav class="nav-content" role="navigation">
|
|
<label for="menuToggler" class="toggler__label">
|
|
<span></span>
|
|
</label>
|
|
<span class="nav-items-wrapper">
|
|
<a href="https://www.python.org/" class="nav-logo">
|
|
<img src="../_static/py.svg" alt="Python logo"/>
|
|
</a>
|
|
<span class="version_switcher_placeholder"></span>
|
|
<form role="search" class="search" action="../search.html" method="get">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
|
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
|
|
</svg>
|
|
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
|
|
<input type="submit" value="Go"/>
|
|
</form>
|
|
</span>
|
|
</nav>
|
|
<div class="menu-wrapper">
|
|
<nav class="menu" role="navigation" aria-label="main navigation">
|
|
<div class="language_switcher_placeholder"></div>
|
|
|
|
<label class="theme-selector-label">
|
|
Theme
|
|
<select class="theme-selector" oninput="activateTheme(this.value)">
|
|
<option value="auto" selected>Auto</option>
|
|
<option value="light">Light</option>
|
|
<option value="dark">Dark</option>
|
|
</select>
|
|
</label>
|
|
<div>
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="base64.html"
|
|
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings</a></p>
|
|
</div>
|
|
<div>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="quopri.html"
|
|
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">quopri</span></code> — Encode and decode MIME quoted-printable data</a></p>
|
|
</div>
|
|
<div role="note" aria-label="source link">
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">Report a Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/main/Doc/library/binascii.rst"
|
|
rel="nofollow">Show Source
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="related" role="navigation" aria-label="Related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="quopri.html" title="quopri — Encode and decode MIME quoted-printable data"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="base64.html" title="base64 — Base16, Base32, Base64, Base85 Data Encodings"
|
|
accesskey="P">previous</a> |</li>
|
|
|
|
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li class="switchers">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<div class="version_switcher_placeholder"></div>
|
|
</li>
|
|
<li>
|
|
|
|
</li>
|
|
<li id="cpython-language-and-version">
|
|
<a href="../index.html">3.13.3 Documentation</a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">Internet Data Handling</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> — Convert between binary and ASCII</a></li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</div>
|
|
|
|
|
</li>
|
|
<li class="right">
|
|
<label class="theme-selector-label">
|
|
Theme
|
|
<select class="theme-selector" oninput="activateTheme(this.value)">
|
|
<option value="auto" selected>Auto</option>
|
|
<option value="light">Light</option>
|
|
<option value="dark">Dark</option>
|
|
</select>
|
|
</label> |</li>
|
|
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<section id="module-binascii">
|
|
<span id="binascii-convert-between-binary-and-ascii"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> — Convert between binary and ASCII<a class="headerlink" href="#module-binascii" title="Link to this heading">¶</a></h1>
|
|
<hr class="docutils" id="index-0" />
|
|
<p>The <a class="reference internal" href="#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code></a> 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
|
|
<a class="reference internal" href="base64.html#module-base64" title="base64: RFC 4648: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> instead. The <a class="reference internal" href="#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code></a> module contains
|
|
low-level functions written in C for greater speed that are used by the
|
|
higher-level modules.</p>
|
|
<div class="admonition note">
|
|
<p class="admonition-title">Note</p>
|
|
<p><code class="docutils literal notranslate"><span class="pre">a2b_*</span></code> functions accept Unicode strings containing only ASCII characters.
|
|
Other functions only accept <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> (such as
|
|
<a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> and other objects that support the buffer
|
|
protocol).</p>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.3: </span>ASCII-only unicode strings are now accepted by the <code class="docutils literal notranslate"><span class="pre">a2b_*</span></code> functions.</p>
|
|
</div>
|
|
</div>
|
|
<p>The <a class="reference internal" href="#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code></a> module defines the following functions:</p>
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.a2b_uu">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_uu</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_uu" title="Link to this definition">¶</a></dt>
|
|
<dd><p>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.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.b2a_uu">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_uu</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">backtick</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_uu" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Convert binary data to a line of ASCII characters, the return value is the
|
|
converted line, including a newline char. The length of <em>data</em> should be at most
|
|
45. If <em>backtick</em> is true, zeros are represented by <code class="docutils literal notranslate"><span class="pre">'`'</span></code> instead of spaces.</p>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>backtick</em> parameter.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.a2b_base64">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_base64</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="positional-only-separator o"><abbr title="Positional-only parameter separator (PEP 570)"><span class="pre">/</span></abbr></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict_mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_base64" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Convert a block of base64 data back to binary and return the binary data. More
|
|
than one line may be passed at a time.</p>
|
|
<p>If <em>strict_mode</em> is true, only valid base64 data will be converted. Invalid base64
|
|
data will raise <a class="reference internal" href="#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a>.</p>
|
|
<p>Valid base64:</p>
|
|
<ul class="simple">
|
|
<li><p>Conforms to <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3548.html"><strong>RFC 3548</strong></a>.</p></li>
|
|
<li><p>Contains only characters from the base64 alphabet.</p></li>
|
|
<li><p>Contains no excess data after padding (including excess padding, newlines, etc.).</p></li>
|
|
<li><p>Does not start with a padding.</p></li>
|
|
</ul>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>strict_mode</em> parameter.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.b2a_base64">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_base64</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_base64" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Convert binary data to a line of ASCII characters in base64 coding. The return
|
|
value is the converted line, including a newline char if <em>newline</em> is
|
|
true. The output of this function conforms to <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3548.html"><strong>RFC 3548</strong></a>.</p>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <em>newline</em> parameter.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.a2b_qp">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_qp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_qp" title="Link to this definition">¶</a></dt>
|
|
<dd><p>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
|
|
<em>header</em> is present and true, underscores will be decoded as spaces.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.b2a_qp">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_qp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">quotetabs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">istext</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_qp" title="Link to this definition">¶</a></dt>
|
|
<dd><p>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
|
|
<em>quotetabs</em> is present and true, all tabs and spaces will be encoded. If the
|
|
optional argument <em>istext</em> is present and true, newlines are not encoded but
|
|
trailing whitespace will be encoded. If the optional argument <em>header</em> is
|
|
present and true, spaces will be encoded as underscores per <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1522.html"><strong>RFC 1522</strong></a>. If the
|
|
optional argument <em>header</em> is present and false, newline characters will be
|
|
encoded as well; otherwise linefeed conversion might corrupt the binary data
|
|
stream.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.crc_hqx">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">crc_hqx</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.crc_hqx" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Compute a 16-bit CRC value of <em>data</em>, starting with <em>value</em> as the
|
|
initial CRC, and return the result. This uses the CRC-CCITT polynomial
|
|
<em>x</em><sup>16</sup> + <em>x</em><sup>12</sup> + <em>x</em><sup>5</sup> + 1, often represented as
|
|
0x1021. This CRC is used in the binhex4 format.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.crc32">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">crc32</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#binascii.crc32" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Compute CRC-32, the unsigned 32-bit checksum of <em>data</em>, starting with an
|
|
initial CRC of <em>value</em>. The default initial CRC is zero. The algorithm
|
|
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:</p>
|
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">binascii</span><span class="o">.</span><span class="n">crc32</span><span class="p">(</span><span class="sa">b</span><span class="s2">"hello world"</span><span class="p">))</span>
|
|
<span class="c1"># Or, in two pieces:</span>
|
|
<span class="n">crc</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">crc32</span><span class="p">(</span><span class="sa">b</span><span class="s2">"hello"</span><span class="p">)</span>
|
|
<span class="n">crc</span> <span class="o">=</span> <span class="n">binascii</span><span class="o">.</span><span class="n">crc32</span><span class="p">(</span><span class="sa">b</span><span class="s2">" world"</span><span class="p">,</span> <span class="n">crc</span><span class="p">)</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'crc32 = </span><span class="si">{:#010x}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">crc</span><span class="p">))</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.0: </span>The result is always unsigned.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.b2a_hex">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_hex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">sep</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">bytes_per_sep=1</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_hex" title="Link to this definition">¶</a></dt>
|
|
<dt class="sig sig-object py" id="binascii.hexlify">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">hexlify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">sep</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">bytes_per_sep=1</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#binascii.hexlify" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Return the hexadecimal representation of the binary <em>data</em>. Every byte of
|
|
<em>data</em> is converted into the corresponding 2-digit hex representation. The
|
|
returned bytes object is therefore twice as long as the length of <em>data</em>.</p>
|
|
<p>Similar functionality (but returning a text string) is also conveniently
|
|
accessible using the <a class="reference internal" href="stdtypes.html#bytes.hex" title="bytes.hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.hex()</span></code></a> method.</p>
|
|
<p>If <em>sep</em> is specified, it must be a single character str or bytes object.
|
|
It will be inserted in the output after every <em>bytes_per_sep</em> input bytes.
|
|
Separator placement is counted from the right end of the output by default,
|
|
if you wish to count from the left, supply a negative <em>bytes_per_sep</em> value.</p>
|
|
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">binascii</span>
|
|
<span class="gp">>>> </span><span class="n">binascii</span><span class="o">.</span><span class="n">b2a_hex</span><span class="p">(</span><span class="sa">b</span><span class="s1">'</span><span class="se">\xb9\x01\xef</span><span class="s1">'</span><span class="p">)</span>
|
|
<span class="go">b'b901ef'</span>
|
|
<span class="gp">>>> </span><span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="sa">b</span><span class="s1">'</span><span class="se">\xb9\x01\xef</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">)</span>
|
|
<span class="go">b'b9-01-ef'</span>
|
|
<span class="gp">>>> </span><span class="n">binascii</span><span class="o">.</span><span class="n">b2a_hex</span><span class="p">(</span><span class="sa">b</span><span class="s1">'</span><span class="se">\xb9\x01\xef</span><span class="s1">'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'_'</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
|
<span class="go">b'b9_01ef'</span>
|
|
<span class="gp">>>> </span><span class="n">binascii</span><span class="o">.</span><span class="n">b2a_hex</span><span class="p">(</span><span class="sa">b</span><span class="s1">'</span><span class="se">\xb9\x01\xef</span><span class="s1">'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">' '</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
|
|
<span class="go">b'b901 ef'</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="versionchanged">
|
|
<p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>sep</em> and <em>bytes_per_sep</em> parameters were added.</p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<dl class="py function">
|
|
<dt class="sig sig-object py" id="binascii.a2b_hex">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_hex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hexstr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_hex" title="Link to this definition">¶</a></dt>
|
|
<dt class="sig sig-object py" id="binascii.unhexlify">
|
|
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">unhexlify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hexstr</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.unhexlify" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Return the binary data represented by the hexadecimal string <em>hexstr</em>. This
|
|
function is the inverse of <a class="reference internal" href="#binascii.b2a_hex" title="binascii.b2a_hex"><code class="xref py py-func docutils literal notranslate"><span class="pre">b2a_hex()</span></code></a>. <em>hexstr</em> must contain an even number
|
|
of hexadecimal digits (which can be upper or lower case), otherwise an
|
|
<a class="reference internal" href="#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> exception is raised.</p>
|
|
<p>Similar functionality (accepting only text string arguments, but more
|
|
liberal towards whitespace) is also accessible using the
|
|
<a class="reference internal" href="stdtypes.html#bytes.fromhex" title="bytes.fromhex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.fromhex()</span></code></a> class method.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="py exception">
|
|
<dt class="sig sig-object py" id="binascii.Error">
|
|
<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#binascii.Error" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Exception raised on errors. These are usually programming errors.</p>
|
|
</dd></dl>
|
|
|
|
<dl class="py exception">
|
|
<dt class="sig sig-object py" id="binascii.Incomplete">
|
|
<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">Incomplete</span></span><a class="headerlink" href="#binascii.Incomplete" title="Link to this definition">¶</a></dt>
|
|
<dd><p>Exception raised on incomplete data. These are usually not programming errors,
|
|
but may be handled by reading a little more data and trying again.</p>
|
|
</dd></dl>
|
|
|
|
<div class="admonition seealso">
|
|
<p class="admonition-title">See also</p>
|
|
<dl class="simple">
|
|
<dt>Module <a class="reference internal" href="base64.html#module-base64" title="base64: RFC 4648: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a></dt><dd><p>Support for RFC compliant base64-style encoding in base 16, 32, 64,
|
|
and 85.</p>
|
|
</dd>
|
|
<dt>Module <a class="reference internal" href="quopri.html#module-quopri" title="quopri: Encode and decode files using the MIME quoted-printable encoding."><code class="xref py py-mod docutils literal notranslate"><span class="pre">quopri</span></code></a></dt><dd><p>Support for quoted-printable encoding used in MIME email messages.</p>
|
|
</dd>
|
|
</dl>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="Main">
|
|
<div class="sphinxsidebarwrapper">
|
|
<div>
|
|
<h4>Previous topic</h4>
|
|
<p class="topless"><a href="base64.html"
|
|
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings</a></p>
|
|
</div>
|
|
<div>
|
|
<h4>Next topic</h4>
|
|
<p class="topless"><a href="quopri.html"
|
|
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">quopri</span></code> — Encode and decode MIME quoted-printable data</a></p>
|
|
</div>
|
|
<div role="note" aria-label="source link">
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">Report a Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/main/Doc/library/binascii.rst"
|
|
rel="nofollow">Show Source
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div id="sidebarbutton" title="Collapse sidebar">
|
|
<span>«</span>
|
|
</div>
|
|
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="Related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="right" >
|
|
<a href="quopri.html" title="quopri — Encode and decode MIME quoted-printable data"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="base64.html" title="base64 — Base16, Base32, Base64, Base85 Data Encodings"
|
|
>previous</a> |</li>
|
|
|
|
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li class="switchers">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<div class="version_switcher_placeholder"></div>
|
|
</li>
|
|
<li>
|
|
|
|
</li>
|
|
<li id="cpython-language-and-version">
|
|
<a href="../index.html">3.13.3 Documentation</a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> — Convert between binary and ASCII</a></li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
|
|
<input type="submit" value="Go" />
|
|
</form>
|
|
</div>
|
|
|
|
|
</li>
|
|
<li class="right">
|
|
<label class="theme-selector-label">
|
|
Theme
|
|
<select class="theme-selector" oninput="activateTheme(this.value)">
|
|
<option value="auto" selected>Auto</option>
|
|
<option value="light">Light</option>
|
|
<option value="dark">Dark</option>
|
|
</select>
|
|
</label> |</li>
|
|
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
©
|
|
<a href="../copyright.html">
|
|
|
|
Copyright
|
|
|
|
</a>
|
|
2001-2025, Python Software Foundation.
|
|
<br />
|
|
This page is licensed under the Python Software Foundation License Version 2.
|
|
<br />
|
|
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
|
<br />
|
|
|
|
See <a href="/license.html">History and License</a> for more information.<br />
|
|
|
|
|
|
<br />
|
|
|
|
The Python Software Foundation is a non-profit corporation.
|
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|
<br />
|
|
<br />
|
|
Last updated on Apr 08, 2025 (14:33 UTC).
|
|
|
|
<a href="/bugs.html">Found a bug</a>?
|
|
|
|
<br />
|
|
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
|
|
</div>
|
|
|
|
</body>
|
|
</html> |