Print a string as hexadecimal bytes

Each Answer to this Q is separated by one/two green lines.

I have this string: Hello, World! and I want to print it using Python as ’48:65:6c:6c:6f:2c:20:57:6f:72:6c:64:21′.

hex() works only for integers.

How can it be done?

You can transform your string to an integer generator. Apply hexadecimal formatting for each element and intercalate with a separator:

>>> s = "Hello, World!"
>>> ":".join("{:02x}".format(ord(c)) for c in s)

':'.join(x.encode('hex') for x in 'Hello, World!')

For Python 2.x:

':'.join(x.encode('hex') for x in 'Hello, World!')

The code above will not work with Python 3.x. For 3.x, the code below will work:

':'.join(hex(ord(x))[2:] for x in 'Hello, World!')

Another answer in two lines that some might find easier to read, and helps with debugging line breaks or other odd characters in a string:

For Python 2.7

for character in string:
    print character, character.encode('hex')

For Python 3.7 (not tested on all releases of 3)

for character in string:
    print(character, character.encode('utf-8').hex())

Some complements to Fedor Gogolev’s answer:

First, if the string contains characters whose ASCII code is below 10, they will not be displayed as required. In that case, the correct format should be {:02x}:

>>> s = "Hello Unicode \u0005!!"
>>> ":".join("{0:x}".format(ord(c)) for c in s)

>>> ":".join("{:02x}".format(ord(c)) for c in s)

Second, if your “string” is in reality a “byte string” — and since the difference matters in Python 3 — you might prefer the following:

>>> s = b"Hello bytes \x05!!"
>>> ":".join("{:02x}".format(c) for c in s)

Please note there is no need for conversion in the above code as a bytes object is defined as “an immutable sequence of integers in the range 0 <= x < 256”.

Print a string as hex bytes?

The accepted answer gives:

s = "Hello world !!"
":".join("{:02x}".format(ord(c)) for c in s)



The accepted answer works only so long as you use bytes (mostly ascii characters). But if you use unicode, e.g.:

a_string = u"?????? ???!!" # "Prevyet mir", or "Hello World" in Russian.

You need to convert to bytes somehow.

If your terminal doesn’t accept these characters, you can decode from UTF-8 or use the names (so you can paste and run the code along with me):

a_string = (

So we see that:

":".join("{:02x}".format(ord(c)) for c in a_string)



a poor/unexpected result – these are the code points that combine to make the graphemes we see in Unicode, from the Unicode Consortium – representing languages all over the world. This is not how we actually store this information so it can be interpreted by other sources, though.

To allow another source to use this data, we would usually need to convert to UTF-8 encoding, for example, to save this string in bytes to disk or to publish to html. So we need that encoding to convert the code points to the code units of UTF-8 – in Python 3, ord is not needed because bytes are iterables of integers:

>>> ":".join("{:02x}".format(c) for c in a_string.encode('utf-8'))

Or perhaps more elegantly, using the new f-strings (only available in Python 3):

>>> ":".join(f'{c:02x}' for c in a_string.encode('utf-8'))

In Python 2, pass c to ord first, i.e. ord(c) – more examples:

>>> ":".join("{:02x}".format(ord(c)) for c in a_string.encode('utf-8'))
>>> ":".join(format(ord(c), '02x') for c in a_string.encode('utf-8'))

You can use hexdump‘s:

import hexdump
hexdump.dump("Hello, World!", sep=":")

(append .lower() if you require lower-case). This works for both Python 2 and 3.

Using map and lambda function can produce a list of hex values, which can be printed (or used for other purposes)

>>> s="Hello 1 2 3 \x01\x02\x03 :)"

>>> map(lambda c: hex(ord(c)), s)
['0x48', '0x65', '0x6c', '0x6c', '0x6f', '0x20', '0x31', '0x20', '0x32', '0x20', '0x33', '0x20', '0x1', '0x2', '0x3', '0x20', '0x3a', '0x29']

A bit more general for those who don’t care about Python¬†3 or colons:

from codecs import encode

data = open('/dev/urandom', 'rb').read(20)
print(encode(data, 'hex'))      # Data

print(encode(b"hello", 'hex'))  # String

This can be done in the following ways:

from __future__ import print_function
str = "Hello, World!"
for char in str:
    mm = int(char.encode('hex'), 16)
    print(hex(mm), sep=':', end=' ')

The output of this will be in hexadecimal as follows:

0x48 0x65 0x6c 0x6c 0x6f 0x20 0x57 0x6f 0x72 0x6c 0x64 0x21

For something that offers more performance than ''.format(), you can use this:

>>> ':'.join( '%02x'%(v if type(v) is int else ord(v)) for v in 'Hello, World!' )
>>> ':'.join( '%02x'%(v if type(v) is int else ord(v)) for v in b'Hello, World!' )

I am sorry this couldn’t look nicer.

It would be nice if one could simply do '%02x'%v, but that only takes int…

But you’ll be stuck with byte-strings b'' without the logic to select ord(v).

With f-string:

"".join(f"{ord(c):x}" for c in "Hello")

Use any delimiter:

>>> "⚡".join(f"{ord(c):x}" for c in "Hello")

Just for convenience, very simple.

def hexlify_byteString(byteString, delim="%"):
    ''' Very simple way to hexlify a byte string using delimiters '''
    retval = ""
    for intval in byteString:
        retval += ('0123456789ABCDEF'[int(intval / 16)])
        retval += ('0123456789ABCDEF'[int(intval % 16)])
        retval += delim

hexlify_byteString(b'Hello, World!', ":")
# Out[439]: '48:65:6C:6C:6F:2C:20:57:6F:72:6C:64:21'

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.