I'm trying to decode a txt file with hex in it. I keep getting a ValueError: non-hexadecimal number found in fromhex() arg at position 2 error
I'm trying to decode a txt file with hex in it. I keep getting a ValueError: non-hexadecimal number found in fromhex() arg at position 2 error
我到处寻找解决方案,但 none 有效。
这是我一直在研究的代码 python 3.
IPFS = open("Q.txt",)
Q = IPFS.read()
print(Q)
IPFS.close()
encoded = bytearray.fromhex(Q).decode()
print(encoded)
Q.txt 文件中有这个字符串。
51
如果您知道是什么让我烦恼,请告诉我,我将不胜感激! :D
我使用 Python 3.9 时效果很好,我得到以下输出:
51
Q
您确定您的文件中没有其他内容吗?或者我不知道...尝试按 Ctrl + A 并删除所有内容,然后重写您的 51
我在 Python 3.8.5 上也能正常工作。我的猜测是你在 1 之后有一个换行符。根据文档,3.7 版中的行为发生了变化:
classmethod fromhex(string)
This bytearray class method returns bytearray object, decoding the given string object. The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.
Changed in version 3.7: bytearray.fromhex() now skips all ASCII whitespace in the string, not just spaces.
我到处寻找解决方案,但 none 有效。 这是我一直在研究的代码 python 3.
IPFS = open("Q.txt",)
Q = IPFS.read()
print(Q)
IPFS.close()
encoded = bytearray.fromhex(Q).decode()
print(encoded)
Q.txt 文件中有这个字符串。
51
如果您知道是什么让我烦恼,请告诉我,我将不胜感激! :D
我使用 Python 3.9 时效果很好,我得到以下输出:
51
Q
您确定您的文件中没有其他内容吗?或者我不知道...尝试按 Ctrl + A 并删除所有内容,然后重写您的 51
我在 Python 3.8.5 上也能正常工作。我的猜测是你在 1 之后有一个换行符。根据文档,3.7 版中的行为发生了变化:
classmethod fromhex(string) This bytearray class method returns bytearray object, decoding the given string object. The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.
Changed in version 3.7: bytearray.fromhex() now skips all ASCII whitespace in the string, not just spaces.