简体   繁体   English

将 HEX 字符串转换为 Python 中的 base64

[英]Convert HEX string into base64 in Python

is it even possible to have an input converted?甚至可以转换输入吗?

from base64 import b64encode, b64decode从 base64 导入 b64encode,b64decode

d = input('What"s your HEX string? ') d = input('你的十六进制字符串是什么?')

b64 = b64encode(bytes.fromhex(d)).decode() b64 = b64encode(bytes.fromhex(d)).decode()

print("Your HEX in base64 is:", b64) print("你在 base64 中的十六进制是:", b64)

**b64 = b64encode(bytes.fromhex(d)).decode()

ValueError: non-hexadecimal number found in fromhex() arg at position 1** ValueError:在 position 1 处的 fromhex() arg 中找到非十六进制数**

After spending a few hours I finally figured it out.花了几个小时后,我终于弄明白了。 Had to alter the b64 var and take out.decode()不得不改变 b64 var 并取出 out.decode()

d = input("Enter HEX here: ")

b64 = b64encode(bytes.fromhex(d))

print("Your HEX in base64 is:", b64)

encodings can be found in codecs module.编码可以在codecs模块中找到。
Try using this尝试使用这个

import codecs
b64 = codecs.encode(codecs.decode(d, 'hex'), 'base64').decode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM