python中十六进制字符串变量到十六进制值的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17206804/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:46:10  来源:igfitidea点击:

Hex string variable to hex value conversion in python

pythonstringhex

提问by drdot

I have a variable call hex_string. The value could be '01234567'. Now I would like to get a hex value from this variable which is 0x01234567 instead of string type. The value of this variable may change. So I need a generic conversion method.

我有一个变量调用 hex_string。该值可能是“01234567”。现在我想从这个变量中获取一个十六进制值,它是 0x01234567 而不是字符串类型。这个变量的值可能会改变。所以我需要一个通用的转换方法。

采纳答案by Jonathon Reinhart

I think you might be mixing up numbers and their representations. 0x01234567and 19088743are the exact same thing. "0x01234567"and "19088743"are not (note the quotes).

我认为您可能混淆了数字及其表示。0x01234567并且19088743是完全一样的东西。"0x01234567"并且"19088743"不是(注意引号)。

To go from a string of hexadecimal characters, to an integer, use int(value, 16).

要将十六进制字符串转换为整数,请使用int(value, 16).

To go from an integer, to a string that represents that number in hex, use hex(value).

要将整数转换为以十六进制表示该数字的字符串,请使用hex(value).

>>> a = 0x01234567
>>> b = 19088743
>>> a == b
True
>>> hex(b)
'0x1234567'
>>> int('01234567', 16)
19088743
>>>

回答by jamylak

>>> int('01234567', 16)
19088743

This is the same as:

这与:

>>> 0x01234567
19088743

回答by rajpy

>>> a
'01234567'
>>> hex(int(a))
'0x12d687'
>>> 

Hope it helps..

希望能帮助到你..

回答by onaclov2000

I don't know the proper 'pythonic' way to do this but here was what I came up with.

我不知道正确的“pythonic”方式来做到这一点,但这是我想出的。

def hex_string_to_bin_string(input):
   lookup = {"0" : "0000", "1" : "0001", "2" : "0010", "3" : "0011", "4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111", "8" : "1000", "9" : "1001", "A" : "1010", "B" : "1011", "C" : "1100", "D" : "1101", "E" : "1110", "F" : "1111"}
   result = ""
   for byte in input:
      result =  result + lookup[byte]
   return result
def hex_string_to_hex_value(input):
   value = hex_string_to_bin_string(input)
   highest_order = len(value) - 1
   result = 0
   for bit in value:
      result = result + int(bit) * pow(2,highest_order)
      highest_order = highest_order - 1
   return hex(result)
print hex_string_to_hex_value("FF")

The result being

结果是

0xff

Also trying print hex_string_to_hex_value("01234567")

还尝试打印 hex_string_to_hex_value("01234567")

results in

结果是

0x1234567L

Note: the L indicates the value falls into category of a "long" as far as I can tell based on the documentation from the python website (they do show that you can have a L value appended). https://docs.python.org/2/library/functions.html#hex

注意:根据 python 网站上的文档,L 表示该值属于“长”类别(它们确实表明您可以附加 L 值)。 https://docs.python.org/2/library/functions.html#hex

>>> hex(255)
'0xff'
>>> hex(-42)
'-0x2a'
>>> hex(1L)
'0x1L'

回答by Tarun Yadav

hex value is <type 'long'>while hex string is <type 'str'>. All operations on hex value are workable if type is changed to long from str.

十六进制值是,<type 'long'>而十六进制字符串是<type 'str'>。如果类型从 str 更改为 long,则对十六进制值的所有操作都是可行的。

long(hex_string,16)