| Server IP : 121.121.20.254 / Your IP : 216.73.217.51 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/Python315/Doc/html/library/ |
Upload File : |
<!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="_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 name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>binascii — Convert between binary and ASCII — Python 3.15.0rc1 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/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=791e38b1"></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.15.0rc1 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>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "library/binascii.rst");
element.href = url.toString();
});
});
</script>
<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 class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/library/binascii.rst?plain=1"
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.15.0rc1 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 <code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> 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 <code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> 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 <code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> 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">padded</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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE64_ALPHABET</span></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>, <em class="sig-param"><span class="n"><span class="pre">canonical</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>
<dt class="sig sig-object py">
<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">ignorechars</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE64_ALPHABET</span></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">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</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></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>Optional <em>alphabet</em> must be a <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> object of length 64 which
specifies an alternative alphabet.</p>
<p>If <em>padded</em> is true, the last group of 4 base 64 alphabet characters must
be padded with the ‘=’ character.
If <em>padded</em> is false, padding is neither required nor recognized:
the ‘=’ character is not treated as padding but as a non-alphabet
character, which means it is silently discarded when <em>strict_mode</em> is false,
or causes 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> when <em>strict_mode</em> is true unless
b’=’ is included in <em>ignorechars</em>.</p>
<p>If <em>ignorechars</em> is specified, it should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>
containing characters to ignore from the input when <em>strict_mode</em> is true.
If <em>ignorechars</em> contains the pad character <code class="docutils literal notranslate"><span class="pre">'='</span></code>, the pad characters
presented before the end of the encoded data and the excess pad characters
will be ignored.
The default value of <em>strict_mode</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>ignorechars</em> is specified,
<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</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/rfc4648.html"><strong>RFC 4648</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>
<p>If <em>canonical</em> is true, non-zero padding bits in the last group are rejected
with <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>, enforcing canonical encoding as defined in
<span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> section 3.5. This check is independent of <em>strict_mode</em>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>strict_mode</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>alphabet</em>, <em>canonical</em>, <em>ignorechars</em>, and <em>padded</em> parameters.</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">padded</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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE64_ALPHABET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></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(s) of ASCII characters in base64 coding,
as specified in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.</p>
<p>If <em>padded</em> is true (default), pad the encoded data with the ‘=’
character to a size multiple of 4.
If <em>padded</em> is false, do not add the pad characters.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not insert any newlines.</p>
<p>If <em>newline</em> is true (default), a newline character will be added
at the end of the output.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <em>newline</em> parameter.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>alphabet</em>, <em>padded</em> and <em>wrapcol</em> parameters.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="binascii.a2b_ascii85">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_ascii85</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">foldspaces</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">adobe</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">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</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_ascii85" title="Link to this definition">¶</a></dt>
<dd><p>Convert Ascii85 data back to binary and return the binary data.</p>
<p>Valid Ascii85 data contains characters from the Ascii85 alphabet in groups
of five (except for the final group, which may have from two to five
characters). Each group encodes 32 bits of binary data in the range from
<code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">**</span> <span class="pre">32</span> <span class="pre">-</span> <span class="pre">1</span></code>, inclusive. The special character <code class="docutils literal notranslate"><span class="pre">z</span></code> is
accepted as a short form of the group <code class="docutils literal notranslate"><span class="pre">!!!!!</span></code>, which encodes four
consecutive null bytes. A single-character final group is always rejected
as an encoding violation.</p>
<p><em>foldspaces</em> is a flag that specifies whether the ‘y’ short sequence
should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
This feature is not supported by the “standard” Ascii85 encoding.</p>
<p><em>adobe</em> controls whether the encoded byte sequence is framed with
<code class="docutils literal notranslate"><span class="pre"><~</span></code> and <code class="docutils literal notranslate"><span class="pre">~></span></code>, as in a PostScript base-85 string literal. If
<em>adobe</em> is true, a leading <code class="docutils literal notranslate"><span class="pre"><~</span></code> is optionally accepted, while a
trailing <code class="docutils literal notranslate"><span class="pre">~></span></code> is <em>required</em>, and <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> is raised
if it is not found.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.
This should only contain whitespace characters.</p>
<p>If <em>canonical</em> is true, non-canonical encodings are rejected with
<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>. Here “canonical” means the encoding that
<a class="reference internal" href="#binascii.b2a_ascii85" title="binascii.b2a_ascii85"><code class="xref py py-func docutils literal notranslate"><span class="pre">b2a_ascii85()</span></code></a> would produce: the <code class="docutils literal notranslate"><span class="pre">z</span></code> abbreviation must be used
for all-zero groups (rather than <code class="docutils literal notranslate"><span class="pre">!!!!!</span></code>), and partial final groups
must use the same padding digits as the encoder.</p>
<p>Invalid Ascii85 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>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="binascii.b2a_ascii85">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_ascii85</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="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">foldspaces</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">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pad</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">adobe</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_ascii85" title="Link to this definition">¶</a></dt>
<dd><p>Convert binary data to a formatted sequence of ASCII characters in Ascii85
coding. The return value is the converted data.</p>
<p><em>foldspaces</em> is an optional flag that uses the special short sequence ‘y’
instead of 4 consecutive spaces (ASCII 0x20) as supported by ‘btoa’. This
feature is not supported by the “standard” Ascii85 encoding.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not insert any newlines.</p>
<p>If <em>pad</em> is true, the zero-padding applied to the end of the input
is fully retained in the output encoding, as done by <code class="docutils literal notranslate"><span class="pre">btoa</span></code>,
producing an exact multiple of 5 bytes of output. This is not part
of the standard encoding used in PDF, as it does not preserve the
length of the data.</p>
<p><em>adobe</em> controls whether the encoded byte sequence is framed with
<code class="docutils literal notranslate"><span class="pre"><~</span></code> and <code class="docutils literal notranslate"><span class="pre">~></span></code>, as in a PostScript base-85 string literal. Note
that while ASCII85Decode streams in PDF documents <em>must</em> be
terminated with <code class="docutils literal notranslate"><span class="pre">~></span></code>, they <em>must not</em> use a leading <code class="docutils literal notranslate"><span class="pre"><~</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="binascii.a2b_base85">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_base85</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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE85_ALPHABET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</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_base85" title="Link to this definition">¶</a></dt>
<dd><p>Convert Base85 data back to binary and return the binary data.
More than one line may be passed at a time.</p>
<p>Valid Base85 data contains characters from the Base85 alphabet in groups
of five (except for the final group, which may have from two to five
characters). Each group encodes 32 bits of binary data in the range from
<code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">**</span> <span class="pre">32</span> <span class="pre">-</span> <span class="pre">1</span></code>, inclusive. A single-character final group is
always rejected as an encoding violation.</p>
<p>Optional <em>alphabet</em> must be a <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> object of length 85 which
specifies an alternative alphabet.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.</p>
<p>If <em>canonical</em> is true, non-canonical encodings are rejected with
<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>. Here “canonical” means the encoding that
<a class="reference internal" href="#binascii.b2a_base85" title="binascii.b2a_base85"><code class="xref py py-func docutils literal notranslate"><span class="pre">b2a_base85()</span></code></a> would produce: partial final groups must use the
same padding digits as the encoder.</p>
<p>Invalid Base85 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>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="binascii.b2a_base85">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_base85</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="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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE85_ALPHABET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pad</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_base85" title="Link to this definition">¶</a></dt>
<dd><p>Convert binary data to a line of ASCII characters in Base85 coding.
The return value is the converted line.</p>
<p>Optional <em>alphabet</em> must be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> of length 85 which
specifies an alternative alphabet.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not insert any newlines.</p>
<p>If <em>pad</em> is true, the zero-padding applied to the end of the input
is retained in the output, which will always be a multiple of 5
bytes, and thus the length of the data may not be preserved on
decoding.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="binascii.a2b_base32">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">a2b_base32</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">padded</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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE32_ALPHABET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</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_base32" title="Link to this definition">¶</a></dt>
<dd><p>Convert base32 data back to binary and return the binary data.</p>
<p>Valid base32 data contains characters from the base32 alphabet specified
in <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> in groups of eight (if necessary, the final group is padded
to eight characters with <code class="docutils literal notranslate"><span class="pre">=</span></code>). Each group encodes 40 bits of binary data
in the range from <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">**</span> <span class="pre">40</span> <span class="pre">-</span> <span class="pre">1</span></code>, inclusive.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This function does not map lowercase characters (which are invalid in
standard base32) to their uppercase counterparts, nor does it
contextually map <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">O</span></code> and <code class="docutils literal notranslate"><span class="pre">1</span></code> to <code class="docutils literal notranslate"><span class="pre">I</span></code>/<code class="docutils literal notranslate"><span class="pre">L</span></code> as <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>
allows.</p>
</div>
<p>Optional <em>alphabet</em> must be a <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> object of length 32 which
specifies an alternative alphabet.</p>
<p>If <em>padded</em> is true, the last group of 8 base 32 alphabet characters must
be padded with the ‘=’ character.
If <em>padded</em> is false, the ‘=’ character is treated as other non-alphabet
characters (depending on the value of <em>ignorechars</em>).</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.
If <em>ignorechars</em> contains the pad character <code class="docutils literal notranslate"><span class="pre">'='</span></code>, the pad characters
presented before the end of the encoded data and the excess pad characters
will be ignored.</p>
<p>If <em>canonical</em> is true, non-zero padding bits in the last group are rejected
with <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>, enforcing canonical encoding as defined in
<span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> section 3.5.</p>
<p>Invalid base32 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>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="binascii.b2a_base32">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">b2a_base32</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="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">padded</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">alphabet</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BASE32_ALPHABET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_base32" title="Link to this definition">¶</a></dt>
<dd><p>Convert binary data to a line of ASCII characters in base32 coding,
as specified in <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>. The return value is the converted line.</p>
<p>Optional <em>alphabet</em> must be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> of length 32 which
specifies an alternative alphabet.</p>
<p>If <em>padded</em> is true (default), pad the encoded data with the ‘=’
character to a size multiple of 8.
If <em>padded</em> is false, do not add the pad characters.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not insert any newlines.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></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-8"></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>, <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">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</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>, <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">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</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><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.</p>
<p>Similar functionality (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>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>ignorechars</em> parameter.</p>
</div>
</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>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.BASE64_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">BASE64_ALPHABET</span></span><a class="headerlink" href="#binascii.BASE64_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The Base 64 alphabet according to <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.URLSAFE_BASE64_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">URLSAFE_BASE64_ALPHABET</span></span><a class="headerlink" href="#binascii.URLSAFE_BASE64_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The “URL and filename safe” Base 64 alphabet according to <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.UU_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">UU_ALPHABET</span></span><a class="headerlink" href="#binascii.UU_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The uuencoding alphabet.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.CRYPT_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">CRYPT_ALPHABET</span></span><a class="headerlink" href="#binascii.CRYPT_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The Base 64 alphabet used in the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/crypt(3)">crypt(3)</a></em> routine and in the GEDCOM format.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.BINHEX_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">BINHEX_ALPHABET</span></span><a class="headerlink" href="#binascii.BINHEX_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The Base 64 alphabet used in BinHex 4 (HQX) within the classic Mac OS.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.BASE85_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">BASE85_ALPHABET</span></span><a class="headerlink" href="#binascii.BASE85_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The Base85 alphabet.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.ASCII85_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">ASCII85_ALPHABET</span></span><a class="headerlink" href="#binascii.ASCII85_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The Ascii85 alphabet.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.Z85_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">Z85_ALPHABET</span></span><a class="headerlink" href="#binascii.Z85_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The <a class="reference external" href="https://rfc.zeromq.org/spec/32/">Z85</a> alphabet.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.BASE32_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">BASE32_ALPHABET</span></span><a class="headerlink" href="#binascii.BASE32_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The Base 32 alphabet according to <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="py data">
<dt class="sig sig-object py" id="binascii.BASE32HEX_ALPHABET">
<span class="sig-prename descclassname"><span class="pre">binascii.</span></span><span class="sig-name descname"><span class="pre">BASE32HEX_ALPHABET</span></span><a class="headerlink" href="#binascii.BASE32HEX_ALPHABET" title="Link to this definition">¶</a></dt>
<dd><p>The “Extended Hex” Base 32 alphabet according to <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.
Data encoded with this alphabet maintains its sort order during bitwise
comparisons.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</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>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "library/binascii.rst");
element.href = url.toString();
});
});
</script>
<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 class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/library/binascii.rst?plain=1"
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.15.0rc1 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 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 Aug 04, 2026 (11:13 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>