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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I don't know the proper 'pythonic' way to do this but here was what I came up with.
The result being
Also trying print hex_string_to_hex_value("01234567")
results in
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
Hope it helps..
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.I think you might be mixing up numbers and their representations.
0x01234567
and19088743
are the exact same thing."0x01234567"
and"19088743"
are not (note the quotes).To go from a string of hexadecimal characters, to an integer, use
int(value, 16)
.To go from an integer, to a string that represents that number in hex, use
hex(value)
.This is the same as: