# Python - convert decimal to ###### tags: `python` ``` def toHex(dec): x = (dec % 16) digits = "0123456789ABCDEF" rest = dec / 16 if (rest == 0): return digits[x] return toHex(rest) + digits[x] numbers = [0, 11, 16, 32, 33, 41, 45, 678, 574893] print [toHex(x) for x in numbers] ['0', 'B', '10', '20', '21', '29', '2D', '2A6', '8C5AD'] print [hex(x) for x in numbers] ['0x0', '0xb', '0x10', '0x20', '0x21', '0x29', '0x2d', '0x2a6', '0x8c5ad'] ``` ``` print "0x%X" % 11 0xB print "0x%X" % 502 0x1F6 print "0x%X" % 1024 0x400 print "0x%X" % 3569 0xDF1 print "%02x" % 127 7f print "%02x" % 3569 df1 ``` ``` print '{0:x}'.format(int(11)) b print '{0:x}'.format(int(502)) 1f6 print '{0:x}'.format(int(1024)) 400 print '{0:x}'.format(int(3569)) df1 ``` ``` def ChangeHex(n): x = (n % 16) c = "" if (x < 10): c = x if (x == 10): c = "A" if (x == 11): c = "B" if (x == 12): c = "C" if (x == 13): c = "D" if (x == 14): c = "E" if (x == 15): c = "F" if (n - x != 0): return ChangeHex(n / 16) + str(c) else: return str(c) print(ChangeHex(11)) B print(ChangeHex(502)) 1F6 print(ChangeHex(1024)) 400 print(ChangeHex(3569)) DF1 ``` ``` print hex(11) 0xb print hex(1024) 0x400 print hex(2456).split('x')[1] 998 print hex(2456)[2:] 998 ```