Python 中的十六进制到 Base64 转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33704327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:49:18  来源:igfitidea点击:

Hex to Base64 conversion in Python

pythonhexbase64

提问by React Native Noob

I want to convert a simple HEX string such as 10000000000002ae to Base 64.

我想将一个简单的十六进制字符串(例如 10000000000002ae)转换为 Base 64。

The hex string is to be converted to bytes, and the bytes are then encoded to base64 notation, so the expected output for that string: EAAAAAAAAq4=

十六进制字符串将被转换为字节,然后字节被编码为 base64 表示法,因此该字符串的预期输出:EAAAAAAAAq4=

I found a tool online. http://tomeko.net/online_tools/hex_to_base64.php?lang=en

我在网上找到了一个工具。http://tomeko.net/online_tools/hex_to_base64.php?lang=en

But I have a bunch of HEX values that I need to convert in a script.

但是我有一堆需要在脚本中转换的十六进制值。

采纳答案by pppery

Python has native support for both HEX and base64 encoding:

Python 原生支持 HEX 和 base64 编码:

encoded = HEX_STRING.decode("hex").encode("base64")

回答by Martijn Pieters

The tool you link to simply interprets the hex as bytes, then encodes those bytes to Base64.

您链接到的工具只是将十六进制解释为字节,然后将这些字节编码为 Base64。

Either use the binascii.unhexlify()functionto convert from a hex string to bytes, or use the bytes.fromhex()class method. Then use the binascii.b2a_base64()functionto convert that to Base64:

要么使用binascii.unhexlify()函数将十六进制字符串转换为字节,要么使用bytes.fromhex()类方法。然后使用该binascii.b2a_base64()函数将其转换为 Base64:

from binascii import unhexlify, b2a_base64

result = b2a_base64(unhexlify(hex_string))

or

或者

from binascii import b2a_base64

result = b2a_base64(bytes.fromhex(hex_string))

In Python 2, you can also use the str.decode()and str.encode()methods to achieve the same:

在 Python 2 中,您还可以使用str.decode()str.encode()方法来实现相同的目的:

result = hex_string.decode('hex').encode('base64')

In Python 3, you'd have to use the codecs.encode()function for this.

在 Python 3 中,您必须为此使用该codecs.encode()函数。

Demo in Python 3:

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'

Demo on Python 2.7:

Python 2.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'

回答by Eana Hufwe

In Python 3, arbitrary encodings including Hex and Base64 has been moved to codecsmodule. To get a Base64 strfrom a hex str:

在 Python 3 中,包括 Hex 和 Base64 在内的任意编码已移至codecs模块。要从str十六进制获取 Base64 str

import codecs

hex = "10000000000002ae"
b64 = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode()

回答by Israel Wenik

Python has native support for both HEX and base64 encoding:

Python 原生支持 HEX 和 base64 编码:

import base64

def main():
    b16 = bytearray('10000000000002ae'.decode('hex'))
    b64 = base64.b64encode(b16)

    print b64

回答by not2qubit

In case someone is looking for a python3 one-liner (bash):

如果有人正在寻找 python3 one-liner (bash):

python -c "import codecs as c; print(c.encode(c.decode('10000000000002ae', 'hex'), 'base64').decode())"

回答by Eds_k

In python3, you may use bytes.fromhexto bytes, use base64 package convert bytes to base64

在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()