简体   繁体   English

Python3字符串到文件中的十六进制错误但在终端中没有

[英]Python3 string to hex error in file but in terminal not

this is my piece of code which outputs at input clues[0] = '706965':这是我在输入线索[0] = '706965' 处输出的代码:

x = clues[0]
answer = bytes.fromhex(x).decode("ascii")
<class 'ValueError'>, ValueError('non-hexadecimal number found in fromhex() arg at position 5')

If I add 0 to to clues[0] it outputs the expected output with first char with offset.如果我将 0 添加到线索 [0] 中,它会输出预期的 output ,其中第一个字符带有偏移量。

x = '0'+clues[0]

The output then is '[]ie'. output 则为“[]ie”。 The [] means special char(stackoverflow ommits it). [] 表示特殊字符(stackoverflow 忽略它)。

but if i run this example in terminal everything works fine.但是如果我在终端运行这个例子,一切正常。

>>> x = '706965'
>>> bytes.fromhex(x).decode("ascii")
'pie'

Solved: Parsing error已解决:解析错误

From the docs for bytes.fromhex来自bytes.fromhex文档

This bytes class method returns a bytes object, decoding the given string object.这个 bytes class 方法返回一个 bytes object,解码给定的字符串 object。 The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.字符串每个字节必须包含两个十六进制数字,忽略 ASCII 空格。

Prepending '0' to your string makes it length seven, so it can't consist of two-digit pairs在您的字符串前面加上 '0' 使其长度为七,因此它不能由两位数对组成

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

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