Conversion.py which takes a decimal number as an input, and returns its representation in a given base as a string.
# conversion.py which takes a decimal number as an input, and returns its representation in a given base as a string (i.e. binary, octal, or hexadecimal string). The function uses a look-up table (dictionary) to map decimal integers to digits in the targeted base lookUp = {0 : '0', 1 : '1', 2 : '2', 3 : '3', 4 :'4', 5 : '5', 6 : '6', 7 : '7', 8 : '8', 9 : '9', 10 : 'A', 11 : 'B', 12 : 'C', 13 : 'D', 14 : 'E', 15 : 'F'} def dec_to_bin(x): return (bin(x)) def dec_to_hex(x): return (hex(x)) def dec_to_oct(x): return (oct(x)) def decimalToRep(decNum,typeVal): if typeVal==2: string=str(decNum) st=list(string) t=[] b='' for i in st: for k,v in lookUp.items(): if i==...
Comments
Post a Comment