在Python中将十六进制字符串转换为int [英] Convert hex string to int in Python
本文介绍了在Python中将十六进制字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Python中将十六进制字符串转换为int?
我可能会把它当作 0xffff
或者只是 ffff $ c如果没有 0x前缀,则需要明确指定基地址,否则,
$ b
x = int(deadbeef,16)
$ c $使用<0x>前缀,Python可以自动区分十六进制和十进制。
$ b >
$ b
>>> print int(0xdeadbeef,0)
3735928559
>>> print int(10,0)
10
<您必须指定 0
作为基础以调用此前缀猜测行为;省略第二个参数表示假设base-10。)
How do I convert a hex string to an int in Python?
I may have it as "0xffff
" or just "ffff
".
解决方案 Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell:
x = int("deadbeef", 16)
With the 0x prefix, Python can distinguish hex and decimal automatically.
>>> print int("0xdeadbeef", 0)
3735928559
>>> print int("10", 0)
10
(You must specify 0
as the base in order to invoke this prefix-guessing behavior; omitting the second parameter means to assume base-10.)
这篇关于在Python中将十六进制字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文