Convert hex string to int in Python
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.)
int(hexString, 16)
完成了这个技巧,并且可以使用和不使用0x前缀:
>>> int("a", 16)
10
>>> int("0xa",16)
10
对于任何给定的字符串s:
int(s, 16)
链接地址: http://www.djcxy.com/p/5436.html