[Tutor] More confusion on conversion

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Oct 30 16:59:50 EST 2003


> length = 10
> convert 10 to A
> convert A to 0x41

Down to here I get but

> padded version = 0x30 0x41

How is this padded to two places? 0x30 is ASCII for 48?
Do you mean 0x20 which is a space? I will assume so...

> told that it wants a single value instead of the two digit 10. How
does
> one convert a 10 (decimal value) to its hex equivalent?

10 and its hex equivalent are the same in the computer.
You display the hex version of 10 using string formating(as one way):

>>> print "%2X" % 10
 A

Note it takes up two characters(the 2 in the fmt string)
and is upper case A(the upper case X).

Now you want the hex codes for those characters:

>>> for c in ' A': print "%X" % ord(c)
20
41

HTH

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld









More information about the Tutor mailing list