[Tutor] how to convert '\xf0' to 0xf0?

Alan Gauld alan.gauld at btinternet.com
Fri Dec 12 10:01:22 CET 2008

What exactly do you want to achieve? I suspect its not what you asked!

To convert the character whose hex value of F0 to an integer value of 
you can use ord() (or just reinterpret the character as a string
using the struct module).

>>> ord('\xf0')

To display the hex value of a character as a hex string you can use
string formatting or the hex() function:

>>> c = '\xF0'
>>> "0x%x" % ord(c)
>>> "0x%X" % ord(c)
>>> hex(ord(c))

Remember that the representation of a number is always a string.
The value of a number is always binary and can be represented in
any base with the default in Python being decimal.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list