[Tutor] Hex to Str - still an open issue

Kent Johnson kent37 at tds.net
Sun Feb 6 13:27:34 CET 2005


Liam Clarke wrote:
> 4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach
> we get 100.

?? 4 (decimal) is 100 (binary). Not because of how the conversion algorithm works, but because that 
is how we write numbers. The least-significant digit is always the rightmost digit. 001 is 1 in 
every number base >= 2.

Actually, generating the digits from the right complicates the algorithm quite a bit. It's hidden in 
the Python version, but s = str(i % 2) + s is a relatively expensive operation here - it has to copy 
all of s to make room for the new digit.

Kent



More information about the Tutor mailing list