How do I convert a hex string to an int in Python?
如何在Python中将十六进制字符串转换为int ?
I may have it as "0xffff
" or just "ffff
".
我可以把它写成“0xffff”或者只是“ffff”。
10 个解决方案
#1
808
Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell:
如果没有0x前缀,就需要显式地指定基数,否则无法判断:
x = int("deadbeef", 16)
With the 0x prefix, Python can distinguish hex and decimal automatically.
使用0x前缀,Python可以自动区分十六进制和十进制。
>>> 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.)
(必须指定0为基数,才能调用这个前缀猜测行为;省略第二个参数表示假设为base-10。
#2
125
int(hexString, 16)
does the trick, and works with and without the 0x prefix:
int(hexString, 16)就是这样做的,使用和不使用0x前缀:
>>> int("a", 16)
10
>>> int("0xa",16)
10
#3
38
For any given string s:
对于给定的字符串s:
int(s, 16)
#4
17
Convert hex string to int in Python
I may have it as
"0xffff"
or just"ffff"
.我可以把它写成“0xffff”或者只是“ffff”。
To convert a string to an int, pass the string to int
along with the base you are converting from.
要将字符串转换为int类型,将字符串传递给int类型以及要转换的基数。
Both strings will suffice for conversion in this way:
两个字符串都可以这样转换:
>>> string_1 = "0xffff"
>>> string_2 = "ffff"
>>> int(string_1, 16)
65535
>>> int(string_2, 16)
65535
Letting int
infer
If you pass 0 as the base, int
will infer the base from the prefix in the string.
如果您将0作为基数传递,int将从字符串中的前缀推断基数。
>>> int(string_1, 0)
65535
Without the hexadecimal prefix, 0x
, int
does not have enough information with which to guess:
没有十六进制前缀0x, int就没有足够的信息来猜测:
>>> int(string_2, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 0: 'ffff'
literals:
If you're typing into source code or an interpreter, Python will make the conversion for you:
如果您正在输入源代码或解释器,Python将为您进行转换:
>>> integer = 0xffff
>>> integer
65535
This won't work with ffff
because Python will think you're trying to write a legitimate Python name instead:
这对ffff不起作用,因为Python会认为您正在尝试编写一个合法的Python名称:
>>> integer = ffff
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ffff' is not defined
Python numbers start with a numeric character, while Python names cannot start with a numeric character.
Python数字以数字字符开头,而Python名称不能以数字字符开头。
#5
11
Adding to Dan's answer above: if you supply the int() function with a hex string, you will have to specify the base as 16 or it will not think you gave it a valid value. Specifying base 16 is unnecessary for hex numbers not contained in strings.
在上面添加Dan的答案:如果您使用十六进制字符串提供int()函数,那么您将不得不指定基数为16,否则它将不会认为您给了它一个有效值。对于字符串中不包含的十六进制数字,不需要指定基数为16。
print int(0xdeadbeef) # valid
myHex = "0xdeadbeef"
print int(myHex) # invalid, raises ValueError
print int(myHex , 16) # valid
#6
3
The worst way:
,最糟糕的莫过于:
>>> def hex_to_int(x):
return eval("0x" + x)
>>> hex_to_int("c0ffee")
12648430
Please don't do this!
Is using eval in Python a bad practice?
在Python中使用eval是不好的实践吗?
#7
1
In Python 2.7, int('deadbeef',10)
doesn't seem to work.
在Python 2.7中,int(“deadbeef”,10)似乎不起作用。
The following works for me:
以下是我的作品:
>>a = int('deadbeef',16)
>>float(a)
3735928559.0
#8
1
The formatter option '%x' % seems to work in assignment statements as well for me. (Assuming Python 3.0 and later)
格式化程序选项'%x' %似乎也适用于赋值语句。(假设Python 3.0及以上版本)
Example
例子
a = int('0x100', 16)
print(a) #256
print('%x' % a) #100
b = a
print(b) #256
c = '%x' % a
print(c) #100
#9
-3
with '0x' prefix, you might also use eval function
有了'0x'前缀,还可以使用eval函数
For example
例如
>>a='0xff'
>>eval(a)
255
#10
-4
To convert a DWORD from hex to a signed integer , implement two's complement like this:
要将一个DWORD从十六进制转换为一个有符号整数,请实现如下的两个补码:
~ (0xffffffff - int('0xdeadbeef', 16)) + 1
#1
808
Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell:
如果没有0x前缀,就需要显式地指定基数,否则无法判断:
x = int("deadbeef", 16)
With the 0x prefix, Python can distinguish hex and decimal automatically.
使用0x前缀,Python可以自动区分十六进制和十进制。
>>> 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.)
(必须指定0为基数,才能调用这个前缀猜测行为;省略第二个参数表示假设为base-10。
#2
125
int(hexString, 16)
does the trick, and works with and without the 0x prefix:
int(hexString, 16)就是这样做的,使用和不使用0x前缀:
>>> int("a", 16)
10
>>> int("0xa",16)
10
#3
38
For any given string s:
对于给定的字符串s:
int(s, 16)
#4
17
Convert hex string to int in Python
I may have it as
"0xffff"
or just"ffff"
.我可以把它写成“0xffff”或者只是“ffff”。
To convert a string to an int, pass the string to int
along with the base you are converting from.
要将字符串转换为int类型,将字符串传递给int类型以及要转换的基数。
Both strings will suffice for conversion in this way:
两个字符串都可以这样转换:
>>> string_1 = "0xffff"
>>> string_2 = "ffff"
>>> int(string_1, 16)
65535
>>> int(string_2, 16)
65535
Letting int
infer
If you pass 0 as the base, int
will infer the base from the prefix in the string.
如果您将0作为基数传递,int将从字符串中的前缀推断基数。
>>> int(string_1, 0)
65535
Without the hexadecimal prefix, 0x
, int
does not have enough information with which to guess:
没有十六进制前缀0x, int就没有足够的信息来猜测:
>>> int(string_2, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 0: 'ffff'
literals:
If you're typing into source code or an interpreter, Python will make the conversion for you:
如果您正在输入源代码或解释器,Python将为您进行转换:
>>> integer = 0xffff
>>> integer
65535
This won't work with ffff
because Python will think you're trying to write a legitimate Python name instead:
这对ffff不起作用,因为Python会认为您正在尝试编写一个合法的Python名称:
>>> integer = ffff
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ffff' is not defined
Python numbers start with a numeric character, while Python names cannot start with a numeric character.
Python数字以数字字符开头,而Python名称不能以数字字符开头。
#5
11
Adding to Dan's answer above: if you supply the int() function with a hex string, you will have to specify the base as 16 or it will not think you gave it a valid value. Specifying base 16 is unnecessary for hex numbers not contained in strings.
在上面添加Dan的答案:如果您使用十六进制字符串提供int()函数,那么您将不得不指定基数为16,否则它将不会认为您给了它一个有效值。对于字符串中不包含的十六进制数字,不需要指定基数为16。
print int(0xdeadbeef) # valid
myHex = "0xdeadbeef"
print int(myHex) # invalid, raises ValueError
print int(myHex , 16) # valid
#6
3
The worst way:
,最糟糕的莫过于:
>>> def hex_to_int(x):
return eval("0x" + x)
>>> hex_to_int("c0ffee")
12648430
Please don't do this!
Is using eval in Python a bad practice?
在Python中使用eval是不好的实践吗?
#7
1
In Python 2.7, int('deadbeef',10)
doesn't seem to work.
在Python 2.7中,int(“deadbeef”,10)似乎不起作用。
The following works for me:
以下是我的作品:
>>a = int('deadbeef',16)
>>float(a)
3735928559.0
#8
1
The formatter option '%x' % seems to work in assignment statements as well for me. (Assuming Python 3.0 and later)
格式化程序选项'%x' %似乎也适用于赋值语句。(假设Python 3.0及以上版本)
Example
例子
a = int('0x100', 16)
print(a) #256
print('%x' % a) #100
b = a
print(b) #256
c = '%x' % a
print(c) #100
#9
-3
with '0x' prefix, you might also use eval function
有了'0x'前缀,还可以使用eval函数
For example
例如
>>a='0xff'
>>eval(a)
255
#10
-4
To convert a DWORD from hex to a signed integer , implement two's complement like this:
要将一个DWORD从十六进制转换为一个有符号整数,请实现如下的两个补码:
~ (0xffffffff - int('0xdeadbeef', 16)) + 1