问题描述
我不知道为什么Hex函数返回的字符串类似于'0x41'而不是0x41
I dont know why Hex function returns a string like '0x41' instead 0x41
我需要将ASCII值转换为十六进制.但是我想要0x INT格式,而不是'0x'字符串.
I need to convert an ASCII value into a hex. But i want in 0x INT format, not into a '0x' string.
ascii = 360
hexstring = hex(ascii)
hexstring += 0x41 # i cant do this because hexstring is a string not a int hex
我如何获取整数十六进制?谢谢
How i can get a int hex??thanks
推荐答案
没有int hex对象.只有替代语法可以创建整数:
There is no int hex object. There is only an alternative syntax to create integers:
>>> 0x41
65
您也可以使用0o1010
来获得相同的值.或使用0b1000001
以二进制形式指定它;它们都是与Python完全相同的数字值;它们都是用于在代码中指定整数值的不同形式.
You could have used 0o1010
too, to get the same value. Or use 0b1000001
to specify it in binary; they are all the exact same numeric value to Python; they are all just different forms to specify an integer value in your code.
只需将ascii
保留为整数,然后将您的十六进制表示法值相加即可:
Simply keep ascii
as an integer and sum your hex notation values with that:
>>> ascii = 360
>>> ascii += 0x41
>>> ascii
425
hex()
生成可由Python程序以相同方式解释的字符串,通常在调试代码或快速演示输出时使用(但如果要生成最终用户输出,则应使用format(number, 'x')
不带0x
前缀).不需要使用整数.
hex()
produces a string that can be interpreted by a Python program in the same manner, and is usually used when debugging code or quick presentation output (but you should use format(number, 'x')
if you want to produce end-user output without the 0x
prefix). It is not needed to work with integers.
这篇关于为什么Hex()函数返回字符串而不是int hex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!