"Luke Paireepinart" <[EMAIL PROTECTED]> wrote > You could also do this by iterating in base-16 instead of base-10...
I was going to suggest the same but using octal which has the same property but fewer values to map. :-) > hexmap = > {"0":"0000","1":"0001","2":"0010","3":"0011","4":"0100","5":"0101", > "6":"0110","7":"0111","8":"1000","9":"1001","a":"1010","b":"1011","c":"1100", > "d":"1101","e":"1110","f":"1111"} > > then for any number n, > you simply do the following: > binary = "" > for x in hex(n)[2:]: > binary += (hexmap[x]) > > this is very simple, but I am unsure how efficient it is. Its very efficient, many of the C standard library routines (tolower etc) use a similar technique. The amount of memory used is low and the speed of a dictionary lookup is much less that multiple divisions etc You can even avoid the dictionary and just use a list for octal since n is the number of the index position, but I don't think list indexing is any/much faster than a dictionary lookup in Python. (Time for timeit() here I think...) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor