Converting strings to hex

Robert Roy rjroy at takingcontrol.com
Fri May 19 12:34:24 EDT 2000


You could also use the SHA module. Gives you a 160 bit vs 128 bit
strength and has a hexdigest() method.  Saves having to write your
own.

Bob


On Fri, 19 May 2000 01:11:05 +0100, "Dale Strickland-Clark"
<dale at out-think.NOSPAMco.uk> wrote:

>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]
>
>a=md5.new(data).digest()
>string.join(map(wibble, list(a)), "")
>
>Which is ugly, at best.
>
>Is there a better way?
>
>hhmm...
>
>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?
>
>Thanks
>
>--
>Dale Strickland-Clark
>Out-Think Ltd, UK
>
>
>




More information about the Python-list mailing list