Dev Notes

Software Development Resources by David Egan.

Convert decimal to hexadecimal in the Python REPL


Python
David Egan

Convert a space-separated string of decimal integers into a space separated string of hexadecimal integers in the Python REPL.

Starting string:

137 60 228 95 213 195 161 98 210 106

# Make a list of integers:
l = "137 60 228 95 213 195 161 98 210 106".split(' ')

# Make a list of hex strings, without leading 0x characters:
h = [str(hex(int(i)))[2:] for i in l]

# Put the list back into a string
hs = ' '.join(h)

# Output:
print(hs)

The final output is:

89 3c e4 5f d5 c3 a1 62 d2 6a

One liner (yikes!):

print(' '.join([str(hex(int(i)))[2:] for i in "137 60 228 95 213 195 161 98 210 106".split(' ')]))

comments powered by Disqus