Python 中的十六进制到 Base64 的转换
Hex to Base64 conversion in Python
我想将一个简单的 HEX 字符串(例如 10000000000002ae
)转换为 Base64。
十六进制字符串将被转换为字节,然后字节被编码为 base64 符号,因此该字符串的预期输出:EAAAAAAAAq4=
我在网上找到了一个工具:http://tomeko.net/online_tools/hex_to_base64.php?lang=en
但是我有一堆十六进制值需要在脚本中转换。
Python 2 原生支持 HEX 和 base64 编码:
encoded = HEX_STRING.decode("hex").encode("base64")
(如果您使用的是 Python 3,请参阅 or 的答案)
您 link 的工具只是将十六进制解释为字节,然后将这些字节编码为 Base64。
要么使用 binascii.unhexlify()
function to convert from a hex string to bytes, or use the bytes.fromhex()
class method. Then use the binascii.b2a_base64()
function 将其转换为 Base64:
from binascii import unhexlify, b2a_base64
result = b2a_base64(unhexlify(hex_string))
或
from binascii import b2a_base64
result = b2a_base64(bytes.fromhex(hex_string))
在Python2中,也可以使用str.decode()
和str.encode()
方法实现同样的效果:
result = hex_string.decode('hex').encode('base64')
在 Python 3 中,您必须为此使用 codecs.encode()
函数。
演示 Python 3:
>>> bytes.fromhex('10000000000002ae')
b'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> from binascii import unhexlify, b2a_base64
>>> unhexlify('10000000000002ae')
b'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> b2a_base64(bytes.fromhex('10000000000002ae'))
b'EAAAAAAAAq4=\n'
>>> b2a_base64(unhexlify('10000000000002ae'))
b'EAAAAAAAAq4=\n'
Python2.7 上的演示:
>>> '10000000000002ae'.decode('hex')
'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> '10000000000002ae'.decode('hex').encode('base64')
'EAAAAAAAAq4=\n'
>>> from binascii import unhexlify, b2a_base64
>>> unhexlify('10000000000002ae')
'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> b2a_base64(unhexlify('10000000000002ae'))
'EAAAAAAAAq4=\n'
编辑 2020 年 8 月 26 日:正如 Ali 在评论中所建议的那样,使用 codecs.encode(b, "base64")
会导致额外的换行符用于 MIME 语法。仅当您确实需要这些换行符格式时才使用此方法。
对于普通 Base64 encoding/decoding,使用 base64.b64encode
和 base64.b64decode
。有关详细信息,请参阅 。
在Python 3中,包括Hex和Base64在内的任意编码已移至codecs
模块。从十六进制 str
得到 Base64 str
:
import codecs
hex = "10000000000002ae"
b64 = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode()
Python 原生支持 HEX 和 base64 编码:
import base64
def main():
b16 = bytearray('10000000000002ae'.decode('hex'))
b64 = base64.b64encode(b16)
print b64
如果有人正在寻找 python3 单线 (bash):
python -c "import codecs as c; print(c.encode(c.decode('10000000000002ae', 'hex'), 'base64').decode())"
在python3中,可以使用bytes.fromhex
转字节,使用base64包将字节转成base64
hex_str = '01'
encoded_str = base64.b64encode(bytes.fromhex(hex_str)).decode('utf-8')
decoded_str = base64.b64decode(encoded_str.encode('utf-8')).hex()
from base64 import b64encode, b64decode
# hex -> base64
s = 'cafebabe'
b64 = b64encode(bytes.fromhex(s)).decode()
print('cafebabe in base64:', b64)
# base64 -> hex
s2 = b64decode(b64.encode()).hex()
print('yv66vg== in hex is:', s2)
assert s == s2
这会打印:
cafebabe in base64: yv66vg==
yv66vg== in hex is: cafebabe
文档中的相关函数,十六进制转base64:
Base64 转十六进制:
我不明白为什么其他许多答案都让它变得如此复杂。例如 截至 2020 年 8 月 26 日:
- 此处不需要
codecs
模块。
codecs
模块在后台使用 base64.encodebytes(s)
(请参阅 reference here), so it converts to multiline MIME base64,因此每输出 76 个字节后就会换行。除非您在 e-mail,这很可能不是你想要的。
至于在编码字符串或解码字节时指定 'utf-8'
:它增加了不必要的噪音。 Python 3 字符串默认使用utf-8编码。标准库的编写者将 encode/decode 方法的默认编码也设为 utf-8 并非巧合,这样您就不必一遍又一遍地指定 utf-8 编码。
我想将一个简单的 HEX 字符串(例如 10000000000002ae
)转换为 Base64。
十六进制字符串将被转换为字节,然后字节被编码为 base64 符号,因此该字符串的预期输出:EAAAAAAAAq4=
我在网上找到了一个工具:http://tomeko.net/online_tools/hex_to_base64.php?lang=en
但是我有一堆十六进制值需要在脚本中转换。
Python 2 原生支持 HEX 和 base64 编码:
encoded = HEX_STRING.decode("hex").encode("base64")
(如果您使用的是 Python 3,请参阅
您 link 的工具只是将十六进制解释为字节,然后将这些字节编码为 Base64。
要么使用 binascii.unhexlify()
function to convert from a hex string to bytes, or use the bytes.fromhex()
class method. Then use the binascii.b2a_base64()
function 将其转换为 Base64:
from binascii import unhexlify, b2a_base64
result = b2a_base64(unhexlify(hex_string))
或
from binascii import b2a_base64
result = b2a_base64(bytes.fromhex(hex_string))
在Python2中,也可以使用str.decode()
和str.encode()
方法实现同样的效果:
result = hex_string.decode('hex').encode('base64')
在 Python 3 中,您必须为此使用 codecs.encode()
函数。
演示 Python 3:
>>> bytes.fromhex('10000000000002ae')
b'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> from binascii import unhexlify, b2a_base64
>>> unhexlify('10000000000002ae')
b'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> b2a_base64(bytes.fromhex('10000000000002ae'))
b'EAAAAAAAAq4=\n'
>>> b2a_base64(unhexlify('10000000000002ae'))
b'EAAAAAAAAq4=\n'
Python2.7 上的演示:
>>> '10000000000002ae'.decode('hex')
'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> '10000000000002ae'.decode('hex').encode('base64')
'EAAAAAAAAq4=\n'
>>> from binascii import unhexlify, b2a_base64
>>> unhexlify('10000000000002ae')
'\x10\x00\x00\x00\x00\x00\x02\xae'
>>> b2a_base64(unhexlify('10000000000002ae'))
'EAAAAAAAAq4=\n'
编辑 2020 年 8 月 26 日:正如 Ali 在评论中所建议的那样,使用 codecs.encode(b, "base64")
会导致额外的换行符用于 MIME 语法。仅当您确实需要这些换行符格式时才使用此方法。
对于普通 Base64 encoding/decoding,使用 base64.b64encode
和 base64.b64decode
。有关详细信息,请参阅
在Python 3中,包括Hex和Base64在内的任意编码已移至codecs
模块。从十六进制 str
得到 Base64 str
:
import codecs
hex = "10000000000002ae"
b64 = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode()
Python 原生支持 HEX 和 base64 编码:
import base64
def main():
b16 = bytearray('10000000000002ae'.decode('hex'))
b64 = base64.b64encode(b16)
print b64
如果有人正在寻找 python3 单线 (bash):
python -c "import codecs as c; print(c.encode(c.decode('10000000000002ae', 'hex'), 'base64').decode())"
在python3中,可以使用bytes.fromhex
转字节,使用base64包将字节转成base64
hex_str = '01'
encoded_str = base64.b64encode(bytes.fromhex(hex_str)).decode('utf-8')
decoded_str = base64.b64decode(encoded_str.encode('utf-8')).hex()
from base64 import b64encode, b64decode
# hex -> base64
s = 'cafebabe'
b64 = b64encode(bytes.fromhex(s)).decode()
print('cafebabe in base64:', b64)
# base64 -> hex
s2 = b64decode(b64.encode()).hex()
print('yv66vg== in hex is:', s2)
assert s == s2
这会打印:
cafebabe in base64: yv66vg== yv66vg== in hex is: cafebabe
文档中的相关函数,十六进制转base64:
Base64 转十六进制:
我不明白为什么其他许多答案都让它变得如此复杂。例如
- 此处不需要
codecs
模块。 codecs
模块在后台使用base64.encodebytes(s)
(请参阅 reference here), so it converts to multiline MIME base64,因此每输出 76 个字节后就会换行。除非您在 e-mail,这很可能不是你想要的。
至于在编码字符串或解码字节时指定 'utf-8'
:它增加了不必要的噪音。 Python 3 字符串默认使用utf-8编码。标准库的编写者将 encode/decode 方法的默认编码也设为 utf-8 并非巧合,这样您就不必一遍又一遍地指定 utf-8 编码。