擅长:python、mysql、java
<p>在所有版本的Python中,可以使用函数
<a href="https://docs.python.org/2/library/binascii.html#binascii.a2b_hex" rel="nofollow noreferrer">^{<cd1>}</a>(也称为<a href="https://docs.python.org/2/library/binascii.html#binascii.unhexlify" rel="nofollow noreferrer">^{<cd2>}</a>):</p>
<pre><code>>>> import binascii
>>> s = '356a192b7913b04c54574d18c28d46e6395428ab'
>>> binascii.a2b_hex(s)
'5j\x19+y\x13\xb0LTWM\x18\xc2\x8dF\xe69T(\xab'
</code></pre>
<p>在Python 3.x中,您可以使用<a href="https://docs.python.org/3/library/stdtypes.html#bytes.fromhex" rel="nofollow noreferrer">^{<cd3>}</a>。</p>
<p>在Python 2.x中,可以使用<code>hex</code>str to str编解码器:</p>
<pre><code>>>> s.decode("hex")
'5j\x19+y\x13\xb0LTWM\x18\xc2\x8dF\xe69T(\xab'
</code></pre>
<p>编解码器内部调用<code>binascii.a2b_hex()</code>。</p>