Conversion to Hex or other bases
Author Message
Conversion to Hex or other bases

How can I convert a decimal to another base? i.e. Hex or binary?

--
Bye,
Gerhard
If you are programming dynamic HTML in python you might find 'template.py'
usefull. Look at my homepage: http://www.*-*-*.com/

Thu, 05 Sep 2002 03:00:00 GMT
Conversion to Hex or other bases
Gerhard Gruber wrote in comp.lang.python:

Quote:
> How can I convert a decimal to another base? i.e. Hex or binary?

This question has been asked several times in the last few days.
I'll just give a quick answer:

"%o" % 31
Gives '37', 31 in octal.

"%x" % 31

There isn't any other way for other bases that I can think of (well, apart
from str(x) to get the decimal :-)). You need to roll your own or look at
the recent posts on this subject for a function that handles any base
up to 36 (10 digits + 26 characters).

--

This is no way to be
Man ought to be free      -- Ted Bundy
That man should be me

Thu, 05 Sep 2002 03:00:00 GMT
Conversion to Hex or other bases
You can get hex or octal string from a decimal number using

1. hex() function :   hex(23)
2. 'formatted string' : hex = "%x" % 23

both gives you '0x17'

Gang

Quote:
> How can I convert a decimal to another base? i.e. Hex or binary?

> --
> Bye,
>    Gerhard
> If you are programming dynamic HTML in python you might find 'template.py'
> usefull. Look at my homepage: http://members.aon.at/sparhawk

Fri, 06 Sep 2002 03:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages