[Tutor] Hex to Str - still an open issue

Alan Gauld alan.gauld at freenet.co.uk
Sun Feb 6 16:10:42 CET 2005


> While I jest somewhat, that highlights a serious deficiency in my
> education that becomes more and more apparent, which is in maths.

Yes, its a sad fact. Good programming beyond basics does require a
modicum of maths. You can learnn enough to do useful things without
math, but there reaches a point when math becomes essential. Its
no coincidence that at university Computing was traditionally
(up till the late 70's at least) a branch of mathematics.

> But the remainder thing - would this be why we read binary the way
we do?
>
> 4 is 001 (on a continuum of 2^0 to 2^n), but using the above
approach
> we get 100.

Not really. The reason we read 4 as 100 is the same reason we
read 400 as 400 instead of 004 - we traditionally put the most
significant part tothe left since we (in English at least) read
from left to right.

400 = 4x10**2 + 0x10**1 + 0x10**0

110 = 1x2**2 + 0x2**1 + 0x2**0

But if we convert back again we can generate the number 400
from the value 400 by the same technique we saw for binary:

400/10 = 40 rem 0
40/10 = 4   rem 0
4/10 = 0    rem 4

So reading remainders bottom up we get 400, which is
the decimal representation of 400! :-)

So the algorithm is identical, we can write a generic
function to convert a value into a representation if we
pass in the value and base.

Alan G.



More information about the Tutor mailing list