在 Python 3 中将字节转换为十六进制字符串的正确方法是什么? [英] What's the correct way to convert bytes to a hex string in Python 3?
本文介绍了在 Python 3 中将字节转换为十六进制字符串的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python 3 中将字节转换为十六进制字符串的正确方法是什么?
我看到了 bytes.hex
方法、bytes.decode
编解码器的声明,并尝试了 其他 最不令人惊讶的可能的功能无济于事.我只想要我的字节为十六进制!
解决方案
自从 Python 3.5 这终于不再尴尬了:
<预><代码>>>>b'xdexadxbexef'.hex()'死牛肉'和反向:
<预><代码>>>>bytes.fromhex('deadbeef')b'xdexadxbexef'也适用于可变的 bytearray
类型.
参考:https://docs.python.org/3/library/stdtypes.html#bytes.hex
What's the correct way to convert bytes to a hex string in Python 3?
I see claims of a bytes.hex
method, bytes.decode
codecs, and have tried other possible functions of least astonishment without avail. I just want my bytes as hex!
解决方案
Since Python 3.5 this is finally no longer awkward:
>>> b'xdexadxbexef'.hex()
'deadbeef'
and reverse:
>>> bytes.fromhex('deadbeef')
b'xdexadxbexef'
works also with the mutable bytearray
type.
Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex
这篇关于在 Python 3 中将字节转换为十六进制字符串的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文