Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Convert hexadecimal string to binary

Thread Tools

Convert hexadecimal string to binary

Posts: n/a
I have the following hexadecimal string - '\xff\xff\xff' which i need
to convert to binary.

How would i go about doing this?

Reply With Quote
Peter Hansen
Posts: n/a
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

Reply With Quote
Harry George
Posts: n/a
      04-22-2004 Removed) (Eric) writes:

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

Assuming you mean the result is a string of "1" and "0" chars, here is
a mechanism. We use a lookup to avoid converting to and from internal

#---x is the data---
#---notice it is actually a sequence of 8-bit bytes---
#---generate a dictionary of all 256 values and their binary codings---

#---collect answer in buffer---
for byte in x:

#---report result---
print s.getvalue()

(E-Mail Removed)
6-6M21 BCA CompArch Design Engineering
Phone: (425) 342-0007
Reply With Quote
Bengt Richter
Posts: n/a
On 22 Apr 2004 08:06:26 -0700, (E-Mail Removed) (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?

Assuming that string is the representation of a string of bytes whose binary
values are 255 (or hex 0xff), and you want to view it like a big-endian
number representation, and convert it to an integer (or long, if it gets big),

>>> def s2i(s): return sum([ord(c)*256**i for i,c in enumerate(s[::-1])])

>>> s2i('\xff\xff\xff')

>>> hex(s2i('\xff\xff\xff'))

>>> hex(s2i('\x01\x02\x03'))

>>> hex(s2i('\x01\x02\x03\x04'))

>>> hex(s2i('\x01\x02\x03\x04\x05'))

>>> hex(s2i('\x01\x02'))

>>> s2i('\x01\x02')


Note that this is unsigned --

>>> hex(s2i('\xff\xff\xff\xff'))

>>> hex(s2i('\x80\x00\x00\x00'))


You could do it other and faster ways...

Bengt Richter
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you convert a hexadecimal number to a binary number? Bob Sanders Ruby 5 05-30-2008 11:50 AM
(BitTorrent-related) binary "string" to hexadecimal? Shanti Braford Ruby 3 10-31-2007 02:04 AM
Binary to Hexadecimal Conversion lei Java 10 12-30-2006 05:02 PM
Convert Binary String to Hexadecimal C Programming 7 12-27-2006 07:41 PM
Hexadecimal to Binary File Conversion Utility Victor Hannak VHDL 0 02-20-2004 08:54 PM