[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 
F0
you can use ord() (or just reinterpret the character as a string
using the struct module).

>>> ord('\xf0')
240

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)
'0xf0'
>>> "0x%X" % ord(c)
'0xF0'
>>> hex(ord(c))
'0xf0'

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.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list