views:

89

answers:

3

Possible Duplicate:
how to parse hex or decimal int in Python

i have a bunch of Hexadecimal colors in a database stored as strings.

e.g. '0xFFFF00'

when i get them from the database i need to convert this string into an actual hexadecimal number, so

0xFFFF00

how can i do this in python

+4  A: 

This is one way to do it:

>>> s = '0xFFFF00'
>>> i = int(s, 16)
>>> print i
Mark Byers
A: 

hex(int('0xFFFF00', 16))

leoluk
This gives exactly the same string, just lowercased -- what do you mean "will do it"?!
Alex Martelli
Probably not the best expression; I didn't see the answer above when I wrote it
leoluk
A: 

Also this works

number = int('0xFFFF00',0)
print("%x follows %x" % (number+1, number))

0 argument tells interpreter to follow the Python rules of numbers to decide the used format of number so this expression will work right for all numbers.

Tony Veijalainen