Domanda

How can I treat a string which is a hexadecimal number as a hexadecimal number? For example, I am loading in a file of hexadecimal numbers but Python is loading the file in as a string. Is it possible to get Python to treat the hexadecimal numbers in the file as hexadecimal numbers? I'm using Python 2.7

Thanks!

È stato utile?

Soluzione

Use int, specifying base-16:

>>> int('FFABCD', 16)
16755661

Edit in reply to comment: OK, misunderstood.

>>> hex(int('0061', 16))
'0x61'

Works I suppose, but I won't be surprised if someone responds with a simpler way.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top