pythontype-conversion

long hex string to integer in python


I receive from a module a string that is a representation of an long int

>>> str = hex(5L)
>>> str
'0x5L'

What I now want is to convert the string str back to a number (integer)

int(str,16) does not work because of the L.

Is there a way to do this without stripping the last L out of the string? Because it is also possible that the string contains a hex without the L ?


Solution

  • You could even just use:

    >>> str = hex(5L)
    >>> long(str,16)
    5L
    >>> int(long(str,16))
    5
    >>>