Search code examples
pythonpython-3.xcasting

hex to dec string in Python with base 10


I have a string in Python which represents a hex val

my_hex_str = "000004fc"

How do I convert it into an int ? 04fc = 1276 in DEC

Tried

my_dec = int(my_hex_str)

But did not work, it gives me

ValueError: invalid literal for int() with base 10: '000004fc'

Solution

  • Specify the base:

    >>> int("000004fc", 16)
    1276