You may be looking for hex(0x2a) == hex(42) == '0x2a'
.
Unless you're looking for a way to convince your existing dumping function to use hexadecimal instead of decimal notation...
Answering to your comment below, if the problem is that you want upper case letters for the hexadecimal digits (but lower case for the 0x
) then you have to use string formatting. You can choose one of the following:
"0x%02X" % 42 # the old way
"0x{:02X}".format(42) == "0x2A" # the new way
In both cases, you'll have to print the 0x
explicitly, followed by a hexadecimal number of at least two digits in upper case, left-padded with a zero if your number has only one digit. This is denoted by the format 02X
, same as in C's printf
.