Python convert decimal to hex
271,772
Solution 1
If you want to code this yourself instead of using the built-in function hex()
, you can simply do the recursive call before you print the current digit:
def ChangeHex(n):
if (n < 0):
print(0)
elif (n<=1):
print n,
else:
ChangeHex( n / 16 )
x =(n%16)
if (x < 10):
print(x),
if (x == 10):
print("A"),
if (x == 11):
print("B"),
if (x == 12):
print("C"),
if (x == 13):
print("D"),
if (x == 14):
print("E"),
if (x == 15):
print ("F"),
Solution 2
What about this:
hex(dec).split('x')[-1]
Example:
>>> d = 30
>>> hex(d).split('x')[-1]
'1e'
~Rich
By using -1 in the result of split(), this would work even if split returned a list of 1 element.
Solution 3
This isn't exactly what you asked for but you can use the "hex" function in python:
>>> hex(15)
'0xf'
Solution 4
I think this solution is elegant:
def toHex(dec):
digits = "0123456789ABCDEF"
x = (dec % 16)
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]
print [hex(x) for x in numbers]
This output:
['0', 'B', '10', '20', '21', '29', '2D', '2A6', '8C5AD']
['0x0', '0xb', '0x10', '0x20', '0x21', '0x29', '0x2d', '0x2a6', '0x8c5ad']
Solution 5
I use
"0x%X" % n
where n
is the decimal number to convert.
Author by
Eric
Updated on July 09, 2022Comments
-
Eric almost 2 years
I have a function here that converts decimal to hex but it prints it in reverse order. How would I fix it?
def ChangeHex(n): if (n < 0): print(0) elif (n<=1): print(n) else: x =(n%16) if (x < 10): print(x), if (x == 10): print("A"), if (x == 11): print("B"), if (x == 12): print("C"), if (x == 13): print("D"), if (x == 14): print("E"), if (x == 15): print ("F"), ChangeHex( n / 16 )
-
Moirisa Dikaiosýni almost 8 yearsprint hex(15); TypeError: 'str' object is not callable
-
Zizouz212 over 7 years@Babbit Then you're doing something wrong. You probably named a variable hex or something.
-
Martin Thoma over 7 yearsSee also: Python: hex conversion always two digits
-
Sven Marnach over 6 years@Apostolos No, I don't think it is. What makes you think so? However, I think it's useful for the original poster of the question to learn how to fix their code, even though it's not the solution i'd recommend to go with in "real" code.
-
sortas over 5 yearsChangeHex( n / 16 ) or ChangeHex( n // 16 ) ?
-
Sven Marnach over 5 years@sortas
n // 16
in Python 3. This answer was written for Python 2, and in that version the two are equivalent. (I tried to make the minimal change to the code in the question to make it work.) -
Germa Vinsmoke about 5 years
-
Meto over 3 yearsPerfect solution, plus the examples. Thank you sir.
-
Admin over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.