Converting strings to hex

Dale Strickland-Clark dale at
Thu May 18 20:11:05 EDT 2000

The output from MD5.digest() is a 16 byte binary string.

I want to convert it to hex for printing.

The hex() function only handles integers (pah!) so I've come up with this:

def wibble(r):
    return hex(ord(r))[2:4]
string.join(map(wibble, list(a)), "")

Which is ugly, at best.

Is there a better way?


I've just discovered unpack and come up with this:

"%4X %4X %4X %4X" % struct.unpack(">4i", a)

which is certainly shorter!

Is this the best I can expect?


Dale Strickland-Clark
Out-Think Ltd, UK

More information about the Python-list mailing list