You mean you have a string of bytes in my_hex which you want to print out as hex numbers, right? E.g., let's take your example:
>>> my_string = "deadbeef"
>>> my_hex = my_string.decode('hex') # python 2 only
>>> print my_hex
Þ ¾ ï
This construction only works on Python 2; but you could write the same string as a literal, in either Python 2 or Python 3, like this:
my_hex = "\xde\xad\xbe\xef"
So, to the answer. Here's one way to print the bytes as hex integers:
>>> print " ".join(hex(ord(n)) for n in my_hex)
0xde 0xad 0xbe 0xef
The comprehension breaks the string into bytes, ord() converts each byte to the corresponding integer, and hex() formats each integer in the from 0x##. Then we add spaces in between.
Bonus: If you use this method with unicode strings (or Python 3 strings), the comprehension will give you unicode characters (not bytes), and you'll get the appropriate hex values even if they're larger than two digits.
Addendum: Byte strings
In Python 3 it is more likely you'll want to do this with a byte string; in that case, the comprehension already returns ints, so you have to leave out the ord() part and simply call hex() on them:
>>> my_hex = b'\xde\xad\xbe\xef'
>>> print(" ".join(hex(n) for n in my_hex))
0xde 0xad 0xbe 0xef
Answer from alexis on Stack OverflowYou mean you have a string of bytes in my_hex which you want to print out as hex numbers, right? E.g., let's take your example:
>>> my_string = "deadbeef"
>>> my_hex = my_string.decode('hex') # python 2 only
>>> print my_hex
Þ ¾ ï
This construction only works on Python 2; but you could write the same string as a literal, in either Python 2 or Python 3, like this:
my_hex = "\xde\xad\xbe\xef"
So, to the answer. Here's one way to print the bytes as hex integers:
>>> print " ".join(hex(ord(n)) for n in my_hex)
0xde 0xad 0xbe 0xef
The comprehension breaks the string into bytes, ord() converts each byte to the corresponding integer, and hex() formats each integer in the from 0x##. Then we add spaces in between.
Bonus: If you use this method with unicode strings (or Python 3 strings), the comprehension will give you unicode characters (not bytes), and you'll get the appropriate hex values even if they're larger than two digits.
Addendum: Byte strings
In Python 3 it is more likely you'll want to do this with a byte string; in that case, the comprehension already returns ints, so you have to leave out the ord() part and simply call hex() on them:
>>> my_hex = b'\xde\xad\xbe\xef'
>>> print(" ".join(hex(n) for n in my_hex))
0xde 0xad 0xbe 0xef
Another answer with later print/format style is:
res[0]=12
res[1]=23
print("my num is 0x{0:02x}{1:02x}".format(res[0],res[1]))
Unusal behavior of python3* print hex values
String to HEX
Difference in print output for hex value (\x escaped) characters between Python 2 and Python 3?
Printing hexadecimals
I am wanting to take a normal string, "My water bottle is blue" for example, and converting that into the HEX representation of how it would be stored in a txt file. I've tried .encode, binascii.hexlify, and a few other things. Any help would be appreciated.
Hello, I'm just trying to understand a difference between Python 2 and Python 3 when printing out values for \x escaped characters. Let me present a simple case where, as I would expect, the output between Python 2 and Python 3 is the same:
# Python 2
print '\x31\x32\x33\x34' # The hex equivalent of '1234'
# Outputs: 1234
# Python 3
print('\x31\x32\x33\x34')
# Outputs: 1234Now here is an example where the output is unexpectedly (to me) different:
# Python 2
print '\xef\xbe\xad\xde' # The hex value 0xdeadbeef (little endian)
# Outputs: ᆳ
# Python 3
print('\xef\xbe\xad\xde')
# Outputs: ï¾ÞWhy, in this case, are the outputs different?