如何将字符串与不可打印的字符转换为python中的十六进制? [英] How to convert byte string with non-printable chars to hexadecimal in python?
问题描述
我有一个ANSI字符串Ď-rór˙rXüď\ő‡íQl7
,我需要像这样将它转换为十六进制:
06cf96f30a7258fcef5cf587ed51156c37
(用XVI32转换)。
问题在于Python无法正确编码所有字符(其中一些字符甚至在此处显示不正确,所以我必须用一个字节字符串来处理它们。
所以上面的字符串是以字节为单位的: b'\\ \\x06\xcf\x96\xf3\\\
r\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'
这就是我需要转换为十六进制的数据。
到目前为止,我尝试了binascii但没有成功,已经试过了:
h =
for i in b'\x06\xcf\ x96 \xf3\\\
r\x83\xffrX\xfc\xef\\\\\ xf5\x87\xedQ\x15l7':
h + = hex(i)
打印(h)
它打印出:
0x60xcf0x960xf30xa0x720x830xff0x720x580xfc0xef0x5c0xf50x870xed0x510x150x6c0x37
好的。它看起来像我在某处......但怎么了与 0x
的东西?
当我删除0x这样的字符串:
h.replace(0x,)
我得到 6cf96f3a7283ff7258fcef5cf587ed51156c37
,看起来是正确的。
但是有时字节串在 有什么想法? 如果你正在运行python 3.5+, 否则,您可以使用 I have an ANSI string The problem is that Python cannot encode all characters correctly (some of them are incorrectly displayed even here, on Stack Overflow) so I have to deal with them with a byte string. So the above string is in bytes this: And that's what I need to convert to hexadecimal. So far I tried binascii with no success, I've tried this: It prints: Okay. It looks like I'm getting somewhere... but what's up with the When I remove 0x from the string like this: I get But sometimes the byte string has a Any ideas? If you're running python 3.5+, Otherwise you can use
这篇关于如何将字符串与不可打印的字符转换为python中的十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! x $ c旁边有一个
0
$ c>并将其从字符串中移除,导致不正确的十六进制字符串。 (上面的字符串在开始处缺少0)。
bytes
type有一个新的 bytes.hex()
方法返回字符串表示形式。
>>> h = b'\x06\xcf\x96\xf3\\\
r\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'
b'\x06\xcf\x96\xf3\\\
r\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'
>>> h.hex()
'06cf96f30a7283ff7258fcef5cf587ed51156c37'
binascii.hexlify()
做同样的事情
>>> import binascii
>>> binascii.hexlify(h).decode('utf8')
'06cf96f30a7283ff7258fcef5cf587ed51156c37'
Ď–ór˙rXüď\ő‡íQl7
and I need to convert it to hexadecimal like this:
06cf96f30a7258fcef5cf587ed51156c37
(converted with XVI32).b'\x06\xcf\x96\xf3\nr\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'
h = ""
for i in b'\x06\xcf\x96\xf3\nr\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7':
h += hex(i)
print(h)
0x60xcf0x960xf30xa0x720x830xff0x720x580xfc0xef0x5c0xf50x870xed0x510x150x6c0x37
0x
thing?h.replace("0x", "")
6cf96f3a7283ff7258fcef5cf587ed51156c37
which looks like it's correct. 0
next to a x
and it gets removed from the string resulting in a incorrect hexadecimal string. (the string above is missing the 0 at the beginning).bytes
type has an new bytes.hex()
method that returns string representation.>>> h = b'\x06\xcf\x96\xf3\nr\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'
b'\x06\xcf\x96\xf3\nr\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'
>>> h.hex()
'06cf96f30a7283ff7258fcef5cf587ed51156c37'
binascii.hexlify()
to do the same thing>>> import binascii
>>> binascii.hexlify(h).decode('utf8')
'06cf96f30a7283ff7258fcef5cf587ed51156c37'