如何将字符串与不可打印的字符转换为python中的十六进制? [英] How to convert byte string with non-printable chars to hexadecimal in python?

查看:220
本文介绍了如何将字符串与不可打印的字符转换为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 ,看起来是正确的。

但是有时字节串在 x 0 $ c>并将其从字符串中移除,导致不正确的十六进制字符串。 (上面的字符串在开始处缺少0)。

有什么想法?

解决方案

如果你正在运行python 3.5+, 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'


I have an ANSI string Ď–ór˙rXüď\ő‡íQl7 and I need to convert it to hexadecimal like this: 06cf96f30a7258fcef5cf587ed51156c37 (converted with XVI32).

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: b'\x06\xcf\x96\xf3\nr\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7'

And that's what I need to convert to hexadecimal.

So far I tried binascii with no success, I've tried this:

h = ""
for i in b'\x06\xcf\x96\xf3\nr\x83\xffrX\xfc\xef\\\xf5\x87\xedQ\x15l7':
    h += hex(i)
print(h)

It prints:

0x60xcf0x960xf30xa0x720x830xff0x720x580xfc0xef0x5c0xf50x870xed0x510x150x6c0x37

Okay. It looks like I'm getting somewhere... but what's up with the 0x thing?

When I remove 0x from the string like this:

h.replace("0x", "")

I get 6cf96f3a7283ff7258fcef5cf587ed51156c37 which looks like it's correct.

But sometimes the byte string has a 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).

Any ideas?

解决方案

If you're running python 3.5+, 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'

Otherwise you can use binascii.hexlify() to do the same thing

>>> import binascii
>>> binascii.hexlify(h).decode('utf8')
'06cf96f30a7283ff7258fcef5cf587ed51156c37'

这篇关于如何将字符串与不可打印的字符转换为python中的十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆