[Tutor] Re: hex output

Abel Daniel abli at freemail.hu
Thu Sep 4 23:55:22 EDT 2003


"Jimmy verma" writes:
> I am having problem in converting hexadecimal no's according to the
> format i require.
> I want to write it in 4 digits form like this  0041
> and also in upper cases.

String formatting can do what you want:
>>> "%04X"%65
'0041'
>>> "%04X"%15
'000F'

For more info (and the meaning of the syntax) see chapter 2.2.6.2 of the
library reference (String Formatting Operations):
http://python.org/doc/current/lib/typesseq-strings.html

Abel Daniel

ps. the above solution pads to number to get a four-digit one, but won't
truncate it, resulting in:
>>> "%04X"%65000000
'3DFD240'
I don't really think you want to truncate (as that will mangle the number,
loosing information).




More information about the Tutor mailing list