Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Base 2 Conversion (See related posts)

Converting from base 2 to int is easy
   1  
   2  >>> int('1010', 2)
   3  10

The opposite is a bit involved.
   1  
   2  >>> number = 1000
   3  >>> hex2bin = {"0":"0000", "1":"0001", "2":"0010", "3":"0011",
   4              "4":"0100", "5":"0101", "6":"0110", "7":"0111",
   5              "8":"1000", "9":"1001", "A":"1010", "B":"1011",
   6              "C":"1100", "D":"1101", "E":"1110", "F":"1111"}
   7  >>> "".join([hex2bin[h] for h in '%X'%number]).lstrip('0')
   8  '1111101000'

See the def of toBase2(number) here

You need to create an account or log in to post comments to this site.


Click here to browse all 5309 code snippets

Related Posts