[Tutor] How can I convert a bin string to a hex string?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Oct 9 14:02:07 EDT 2003



On Thu, 9 Oct 2003, rafael.sousa wrote:

> Such as the binary contents of a jpeg image?
>  I'm writing the code to dynamically create a RTF (Rich Text Format)
> document. In order to insert a picture, it's recommended in the
> specification that its contents are written in hex.
>  I read the binary content to a regular python string, but then I would
> need to convert it to another string, this time in hex (which would
> naturally have the double of the length of the original bin string).


Hi Rafael,


Ah, then the 'binascii' module should be suitable for this:

    http://www.python.org/doc/lib/module-binascii.html

The binascii.hexlify() function should be exactly what you're looking for.



Good luck!




More information about the Tutor mailing list