Python 如何将十六进制字符串转换为十六进制数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3904135/
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
How to convert hex string to hex number?
提问by Rise
I have integer number in ex. 16 and i am trying to convert this number to a hex number. I tried to achieve this by using hex function but whenever you provide a integer number to the hex function it returns string representation of hex number,
我在 ex 中有整数。16 我正在尝试将此数字转换为十六进制数字。我试图通过使用十六进制函数来实现这一点,但是每当您向十六进制函数提供整数时,它都会返回十六进制数的字符串表示形式,
my_number = 16
hex_no = hex(my_number)
print type(hex_no) // It will print type of hex_no as str.
Can someone please tell me how to convert hex number in string format to simply a hex number.
Thanks!!
有人可以告诉我如何将字符串格式的十六进制数转换为简单的十六进制数。
谢谢!!
采纳答案by Ignacio Vazquez-Abrams
>>> print int('0x10', 16)
16
回答by Cheers and hth. - Alf
Your code works for me, no apostrophes added.
你的代码对我有用,没有添加撇号。
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> my_number = 16
>>> hex_no = hex(my_number)
>>> print hex_no
0x10
>>> _
Note, by the way, that there's no such thing as a "hex number". Hex is just a way to specifya number value. In the computer's memory that number value is usually represented in binary, no matter how it's specified in your source code (decimal, hex, whatever).
请注意,顺便说一下,没有“十六进制数”这样的东西。十六进制只是指定数字值的一种方式。在计算机的内存中,该数值通常以二进制表示,无论它在源代码中如何指定(十进制、十六进制等)。
Cheers & hth.,
干杯 & hth.,
– Alf
– 阿尔夫
回答by Emil
Sample Code :
示例代码:
print "%x"%int("2a",16)
回答by The Archetypal Paul
Are you asking how to convert the string format hexadecimal value '16' into an integer (that is, end up with an integer with decimal value 22)? It's not clear from your question. If so, you probably want int('16', 16)
请问如何将字符串格式的十六进制值'16'转成整数(即以十进制值22结尾的整数)?从你的问题看不清楚。如果是这样,您可能需要 int('16', 16)
回答by poke
Using the string formatters (new first, then old):
使用字符串格式化程序(先新,后旧):
>>> '{:x}'.format( 12345678 )
'bc614e'
>>> '%x' % ( 12345678 )
'bc614e'
回答by luc
With Python 2.6.5 on MS Windows Vista, the command line interpreter behaves this way:
对于 MS Windows Vista 上的 Python 2.6.5,命令行解释器的行为方式如下:
>>>hex(16)
'0x10'
>>>print hex(16)
0x10
I guess this is the normal behavior:
我想这是正常行为:
>>>'abc'
'abc'
>>>print 'abc'
abc
I hope it helps
我希望它有帮助
回答by Rasmi Ranjan Nayak
def add_hex2(hex1, hex2):
"""add two hexadecimal string values and return as such"""
return hex(int(hex1, 16) + int(hex2, 16))
print add_hex2('0xff', '0xff') # 0x1fe
回答by Abdulvakaf K
I think here most of the answers were misinterpreted or they understood the question wrongly.
我认为这里的大多数答案都被误解了,或者他们错误地理解了这个问题。
To answer to your question it is IMPOSSIBLE to convert resultant string representation of Hex data to Hex numbers(integer representation).
要回答您的问题,将十六进制数据的结果字符串表示形式转换为十六进制数字(整数表示形式)是不可能的。
Because, when you convert an integer to hex by doing following
因为,当您通过执行以下操作将整数转换为十六进制时
>>> a = hex(34)
>>> print type(a)
<type 'str'>
>>> print a
0x22
>>> a
'0x22'
And some answers were confused here,
一些答案在这里很混乱,
>>> print a
0x22
>>> a
'0x22'
When you type print ain interpreter it will result the string data WITHOUTquotes and If you simply type the variable name without using print statement then it would print the string data WITHsingle/double quotes.
当您键入打印在解释将导致字符串数据就无报价,如果你只是没有使用print语句输入变量名称,然后将打印字符串数据与单/双引号。
Though the resultant value is Hex databut the representation is in STRING.
虽然结果值是十六进制数据,但表示是在STRING 中。
As per Python docs you cannot convert to Hex number as I told earlier.
根据 Python 文档,您不能像我之前所说的那样转换为十六进制数。
Thanks.
谢谢。

