42

Does anyone know how to get a chr to hex conversion where the output is always two digits?

for example, if my conversion yields 0x1, I need to convert that to 0x01, since I am concatenating a long hex string.

The code that I am using is:

hexStr += hex(ord(byteStr[i]))[2:]

7 Answers 7

69

You can use string formatting for this purpose:

>>> "0x{:02x}".format(13)
'0x0d'

>>> "0x{:02x}".format(131)
'0x83'

Edit: Your code suggests that you are trying to convert a string to a hexstring representation. There is a much easier way to do this (Python2.x):

>>> "abcd".encode("hex")
'61626364'

An alternative (that also works in Python 3.x) is the function binascii.hexlify().

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This worked, except that I had to drop the 0x from the beginning, since all I wanted was the part after that.
Also note that if you want 0x0D vs 0x0d use X vs x in format string, ie, "0x{:02X}".format(13)
"0x{:02x}".format(239557639) result '0xe475c07' wrong
22

You can use the format function:

>>> format(10, '02x')
'0a'

You won't need to remove the 0x part with that (like you did with the [2:])

2 Comments

Thank you. This is a nice idea. However, I can't get it work since my output is a string, and this seems to work only for integers. I'm pretty new to python. Is there a way around this?
The string could be casted into an integer in the following manner format(string("10"), '02x')
13

If you're using python 3.6 or higher you can also use fstrings:

v = 10
s = f"0x{v:02x}"
print(s)

output:

0x0a

The syntax for the braces part is identical to string.format(), except you use the variable's name. See https://www.python.org/dev/peps/pep-0498/ for more.

1 Comment

This is useful, particularly when we don't need print, but as strings to manipulate.
6
htmlColor = "#%02X%02X%02X" % (red, green, blue)

Comments

4

The standard module binascii may also be the answer, namely when you need to convert a longer string:

>>> import binascii
>>> binascii.hexlify('abc\n')
'6162630a'

Comments

2

Use format instead of using the hex function:

>>> mychar = ord('a')
>>> hexstring = '%.2X' % mychar

You can also change the number "2" to the number of digits you want, and the "X" to "x" to choose between upper and lowercase representation of the hex alphanumeric digits.

By many, this is considered the old %-style formatting in Python, but I like it because the format string syntax is the same used by other languages, like C and Java.

Comments

2

The simpliest way (I think) is:

your_str = '0x%02X' % 10
print(your_str)

will print:

0x0A

The number after the % will be converted to hex inside the string, I think it's clear this way and from people that came from a C background (like me) feels more like home

3 Comments

I would note that the output using your_str = '0x%02X,' % 10 has a comma in it. 0x0A,
That's true, I must have copied it from some code I was working and didn't clean it very well. Thanks for the catch.
I actually found that example useful because I wanted a comma :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.