Convert hexadecimal string to binary

Peter Hansen peter at engcorp.com
Thu Apr 22 11:42:38 EDT 2004


Eric wrote:

> I have the following hexadecimal string - '\xff\xff\xff' which i need
> to convert to binary.
> 
> How would i go about doing this?

You'll need to explain more clearly, and possibly change how you
view such things, before anyone can help you with certainty.

What you show above is a *binary* sequence of bytes containing
three bytes, each with the value 255.  The representation of
it which you copied (?) from the console contains printable
characters and extra stuff to make it easier to read, but that's
what you get from repr() on a binary string.

Do you actually want a string containing the six printable
characters 'FFFFFF'?  If that's the case, it's not what you
would usually call "binary".  Anyway, use binascii.hexlify()
for that...

Can you give an example of the output you really want?  How
many bytes does it contain?  How does it differ from the string
above?

-Peter



More information about the Python-list mailing list