Search code examples
pythonpython-2.7hexdecimalstring-conversion

Python: convert string of multiple hexadecimal values to integer


I am having string of four hex numbers like:

"0x00 0x01 0x13 0x00" 

which is equivalent to 0x00011300. How i can convert this hex value to integer?


Solution

  • Since you are having string of hex values. You may remove ' 0x' from the string to get the single hex number string. For example:

    my_str =  "0x00 0x01 0x13 0x00"
    hex_str = my_str.replace(' 0x', '')
    

    where value hold by hex_str will be:

    '0x00011300'   # `hex` string
    

    Now you may convert hex string to int as:

    >>> int(hex_str, 16)
    70400