[Tutor] hex output
tpc at csua.berkeley.edu
tpc at csua.berkeley.edu
Thu Sep 4 15:16:26 EDT 2003
hi Jimmy, for the upper casing you use the upper() function like so:
<paste>
>>> hex(3735928559).upper()
'0XDEADBEEFL'
</paste>
the L means long type. Now if you require all your decimal to hex
conversions to be in the form 0000 without the 0x or L signifiers you
might write a function which takes any hexstring, removes the 0x and L,
checks the length of the hexstring and does the appropriate thing:
<paste>
>>> def dec2hexconverter(x):
hexstring = hex(x).replace('0x', '').replace('L', '').upper()
length = len(hexstring)
if length == 1:
hexstring = '000' + hexstring
elif length == 2:
hexstring = '00' + hexstring
elif length == 3:
hexstring = '0' + hexstring
elif length == 8:
hexstring = hexstring[:4] + ' ' + hexstring[-4:]
return hexstring
>>> dec2hexconverter(3735928559)
'DEAD BEEF'
</paste>
Granted, not the most elegant solution, but that should get you started.
On Thu, 4 Sep 2003, Jimmy verma wrote:
> Hello *,
>
> I am having problem in converting hexadecimal no's according to the format i
> require.
>
> >>>hex(65)
> '0x41'
> >>>hex(65)[2:]
> '41'
>
>
> I want to write it in 4 digits form like this 0041
> and also in upper cases.
>
> Your suggestion are welcomed.
>
> Thanks a lot.
>
> Regards,
>
> J+
>
> _________________________________________________________________
> Need a naukri? Your search ends here. http://www.msn.co.in/naukri/ 50,000 of
> the best jobs!
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list