python 如何将 '\x01' 设为 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1411658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:10:50  来源:igfitidea点击:

How to get '\x01' to 1

python

提问by steprobe

I am getting this:

我得到这个:

_format_ = "7c7sc"
print struct.unpack(self._format_, data)

gives

('\x7f', 'E', 'L', 'F', '\x01', '\x01', '\x01', '\x00\x00\x00\x00\x00\x00\x00', '\x00')

I want to take '\x01'and get 1 from it, i.e., convert to ``int. Any ideas? Thanks

我想从中获取'\x01'并获得 1,即转换为 ``int。有任何想法吗?谢谢

回答by sepp2k

ord("\x01")will return 1.

ord("\x01")将返回 1。

回答by gahooa

Perhaps you are thinking of the ordfunction?

也许您正在考虑ord功能?

>>> ord("\x01")
1
>>> ord("\x02")
2
>>> ord("\x7f")
127