413 lines
31 KiB
HTML
413 lines
31 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>binascii --- 二进制和 ASCII 码互转 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="quopri --- 编码与解码经过 MIME 转码的可打印数据" href="quopri.html" />
|
||
<link rel="prev" title="binhex --- 对binhex4文件进行编码和解码" href="binhex.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="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>
|
||
|
||
</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" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<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"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="binhex.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">binhex</span></code> --- 对binhex4文件进行编码和解码</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="quopri.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">quopri</span></code> --- 编码与解码经过 MIME 转码的可打印数据</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/binascii.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/binascii.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="quopri.html" title="quopri --- 编码与解码经过 MIME 转码的可打印数据"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="binhex.html" title="binhex --- 对binhex4文件进行编码和解码"
|
||
accesskey="P">上一页</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.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">互联网数据处理</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</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><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> --- 二进制和 ASCII 码互转<a class="headerlink" href="#module-binascii" title="永久链接至标题">¶</a></h1>
|
||
<hr class="docutils" id="index-0" />
|
||
<p><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> 模块包含很多在二进制和二进制表示的各种ASCII码之间转换的方法。 通常情况不会直接使用这些函数,而是使用像 <a class="reference internal" href="uu.html#module-uu" title="uu: Encode and decode files in uuencode format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">uu</span></code></a> , <a class="reference internal" href="base64.html#module-base64" title="base64: RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> ,或 <a class="reference internal" href="binhex.html#module-binhex" title="binhex: Encode and decode files in binhex4 format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binhex</span></code></a> 这样的封装模块。 为了执行效率高,<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> 模块含有许多用 C 写的低级函数,这些底层函数被一些高级模块所使用。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p><code class="docutils literal notranslate"><span class="pre">a2b_*</span></code> 函数接受只含有 ASCII 码的Unicode 字符串。其他函数只接受 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">字节类对象</span></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>,<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> 和其他支持缓冲区协议的对象)。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 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><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> 模块定义了以下函数:</p>
|
||
<dl class="function">
|
||
<dt id="binascii.a2b_uu">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">a2b_uu</code><span class="sig-paren">(</span><em class="sig-param">string</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_uu" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将单行 uu 编码数据转换成二进制数据并返回。uu 编码每行的数据通常包含45 个(二进制)字节,最后一行除外。每行数据后面可能跟有空格。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.b2a_uu">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">b2a_uu</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">*</em>, <em class="sig-param">backtick=False</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_uu" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将二进制数据转换为 ASCII 编码字符,返回值是转换后的行数据,包括换行符。 <em>data</em> 的长度最多为45。如果 <em>backtick</em> 为ture,则零由 <code class="docutils literal notranslate"><span class="pre">'`'</span></code> 而不是空格表示。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>增加 <em>backtick</em> 形参。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.a2b_base64">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">a2b_base64</code><span class="sig-paren">(</span><em class="sig-param">string</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_base64" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将 base64 数据块转换成二进制并以二进制数据形式返回。一次可以传递多行数据。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.b2a_base64">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">b2a_base64</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">*</em>, <em class="sig-param">newline=True</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_base64" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将二进制数据转换为一行用 base64 编码的ASCII字符串。返回值是转换后的行数据,如果 <em>newline</em> 为true,则返回值包括换行符。该函数的输出符合:rfc:<cite>3548</cite>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>增加 <em>newline</em> 形参。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.a2b_qp">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">a2b_qp</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">header=False</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_qp" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将一个引号可打印的数据块转换成二进制数据并返回。一次可以转换多行。如果可选参数 <em>header</em> 存在且为true,则数据中的下划线将被解码成空格。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.b2a_qp">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">b2a_qp</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">quotetabs=False</em>, <em class="sig-param">istext=True</em>, <em class="sig-param">header=False</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_qp" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将二进制数据转换为一行或多行带引号可打印编码的ASCII字符串。返回值是转换后的行数据。如果可选参数 <em>quotetabs</em> 存在且为真值,则对所有制表符和空格进行编码。如果可选参数 <em>istext</em> 存在且为真值,则不对新行进行编码,但将对尾随空格进行编码。如果可选参数 <em>header</em> 存在且为true,则空格将被编码为下划线 <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1522.html"><strong>RFC 1522</strong></a>。如果可选参数 <em>header</em> 存在且为假值,则也会对换行符进行编码;不进行换行转换编码可能会破坏二进制数据流。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.a2b_hqx">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">a2b_hqx</code><span class="sig-paren">(</span><em class="sig-param">string</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_hqx" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将 binhex4 格式的 ASCII 数据不进行 RLE 解压缩直接转换为二进制数据。该字符串应包含完整数量的二进制字节,或者(在binhex4 数据最后部分)剩余位为零。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.rledecode_hqx">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">rledecode_hqx</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.rledecode_hqx" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>根据 binhex4 标准对数据执行 RLE 解压缩。该算法在一个字节的数据后使用 <code class="docutils literal notranslate"><span class="pre">0x90</span></code> 作为重复指示符,然后计数。计数 <code class="docutils literal notranslate"><span class="pre">0</span></code> 指定字节值 <code class="docutils literal notranslate"><span class="pre">0x90</span></code> 。该例程返回解压缩的数据,输入数据以孤立的重复指示符结束的情况下,将引发 <a class="reference internal" href="#binascii.Incomplete" title="binascii.Incomplete"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Incomplete</span></code></a> 异常。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>仅接受 bytestring 或 bytearray 对象作为输入。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.rlecode_hqx">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">rlecode_hqx</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.rlecode_hqx" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在 <em>data</em> 上执行 binhex4 游程编码压缩并返回结果。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.b2a_hqx">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">b2a_hqx</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_hqx" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>执行 hexbin4 类型二进制到 ASCII 码的转换并返回结果字符串。输入数据应经过 RLE 编码,且数据长度可被3整除(除了最后一个片段)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.crc_hqx">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">crc_hqx</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.crc_hqx" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>以 <em>value</em> 作为初始 CRC 计算 <em>data</em> 的16位 CRC 值,返回其结果。这里使用 CRC-CCITT 生成多项式 <em>x</em><sup>16</sup> + <em>x</em><sup>12</sup> + <em>x</em><sup>5</sup> + 1 ,通常表示为0x1021。该 CRC 被用于 binhex4 格式。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.crc32">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">crc32</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="optional">[</span>, <em class="sig-param">value</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#binascii.crc32" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>计算 CRC-32 ,从 <em>value</em> 的初始 CRC 开始计算 <em>data</em> 的32位校验和。默认初始 CRC 为零。该算法与 ZIP 文件校验和一致。由于该算法被设计用作校验和算法,因此不适合用作通用散列算法。使用方法如下:</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">在 3.0 版更改: </span>校验结果始终是无符号类型的。要在所有Python版本和平台上生成相同的数值,请使用 <code class="docutils literal notranslate"><span class="pre">crc32(data)</span> <span class="pre">&</span> <span class="pre">0xffffffff</span></code> 。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.b2a_hex">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">b2a_hex</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="optional">[</span>, <em class="sig-param">sep</em><span class="optional">[</span>, <em class="sig-param">bytes_per_sep=1</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#binascii.b2a_hex" title="永久链接至目标">¶</a></dt>
|
||
<dt id="binascii.hexlify">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">hexlify</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="optional">[</span>, <em class="sig-param">sep</em><span class="optional">[</span>, <em class="sig-param">bytes_per_sep=1</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#binascii.hexlify" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回二进制数据 <em>data</em> 的十六进制表示形式。 <em>data</em> 的每个字节都被转换为相应的2位十六进制表示形式。因此返回的字节对象的长度是 <em>data</em> 的两倍。</p>
|
||
<p>使用:<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> 方法也可以方便地实现相似的功能(但仅返回文本字符串)。</p>
|
||
<p>如果指定了 <em>sep</em>,它必须为单字符 str 或 bytes 对象。 它将被插入每个 <em>bytes_per_sep</em> 输入字节之后。 分隔符位置默认从输出的右端开始计数,如果你希望从左端开始计数,请提供一个负的 <em>bytes_per_sep</em> 值。</p>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</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">在 3.8 版更改: </span>添加了 <em>sep</em> 和 <em>bytes_per_sep</em> 形参。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="binascii.a2b_hex">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">a2b_hex</code><span class="sig-paren">(</span><em class="sig-param">hexstr</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.a2b_hex" title="永久链接至目标">¶</a></dt>
|
||
<dt id="binascii.unhexlify">
|
||
<code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">unhexlify</code><span class="sig-paren">(</span><em class="sig-param">hexstr</em><span class="sig-paren">)</span><a class="headerlink" href="#binascii.unhexlify" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回由十六进制字符串 <em>hexstr</em> 表示的二进制数据。此函数功能与 <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> 必须包含偶数个十六进制数字(可以是大写或小写),否则会引发 <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> 异常。</p>
|
||
<p>使用:<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> 类方法也实现相似的功能(仅接受文本字符串参数,不限制其中的空白字符)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="binascii.Error">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">Error</code><a class="headerlink" href="#binascii.Error" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>通常是因为编程错误引发的异常。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="binascii.Incomplete">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">binascii.</code><code class="sig-name descname">Incomplete</code><a class="headerlink" href="#binascii.Incomplete" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>数据不完整引发的异常。通常不是编程错误导致的,可以通过读取更多的数据并再次尝试来处理该异常。</p>
|
||
</dd></dl>
|
||
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt>模块 <a class="reference internal" href="base64.html#module-base64" title="base64: RFC 3548: 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>支持在16,32,64,85进制中进行符合 RFC 协议的 base64 样式编码。</p>
|
||
</dd>
|
||
<dt>模块 <a class="reference internal" href="binhex.html#module-binhex" title="binhex: Encode and decode files in binhex4 format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binhex</span></code></a></dt><dd><p>支持在 Macintosh 上使用的 binhex 格式。</p>
|
||
</dd>
|
||
<dt>模块 <a class="reference internal" href="uu.html#module-uu" title="uu: Encode and decode files in uuencode format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">uu</span></code></a></dt><dd><p>支持 Unix 使用的 UU 编码。</p>
|
||
</dd>
|
||
<dt>模块 <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>支持在 MIME 版本电子邮件中使用引号可打印编码。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="binhex.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">binhex</span></code> --- 对binhex4文件进行编码和解码</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="quopri.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">quopri</span></code> --- 编码与解码经过 MIME 转码的可打印数据</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/binascii.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="quopri.html" title="quopri --- 编码与解码经过 MIME 转码的可打印数据"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="binhex.html" title="binhex --- 对binhex4文件进行编码和解码"
|
||
>上一页</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.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="netdata.html" >互联网数据处理</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, 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 />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |