Convert hexadecimal string to binary

Discussion in 'Python' started by Eric, Apr 22, 2004.

  1. Eric

    Eric Guest

    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
     
    Eric, Apr 22, 2004
    #1
    1. Advertising

  2. Eric

    Peter Hansen Guest

    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
     
    Peter Hansen, Apr 22, 2004
    #2
    1. Advertising

  3. Eric

    Harry George Guest

    (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
    numbers.

    #---x is the data---
    x='\xff\xff\xff'
    #---notice it is actually a sequence of 8-bit bytes---
    #---generate a dictionary of all 256 values and their binary codings---
    bits={'\x00':'00000000',
    '\x01':'00000001',
    ..........
    '\xff':'11111111'}

    #---collect answer in buffer---
    s=StringIO.StringIO()
    for byte in x:
    s.write(bits[byte])

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





    --

    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 342-0007
     
    Harry George, Apr 22, 2004
    #3
  4. On 22 Apr 2004 08:06:26 -0700, (Eric) wrote:

    >Guys,
    >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')

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

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

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

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

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

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

    258

    Note that this is unsigned --

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

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

    '0x80000000L'

    You could do it other and faster ways...

    Regards,
    Bengt Richter
     
    Bengt Richter, Apr 22, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Hannak
    Replies:
    0
    Views:
    7,521
    Victor Hannak
    Feb 20, 2004
  2. lei
    Replies:
    10
    Views:
    812
    Don Roby
    Dec 30, 2006
  3. Replies:
    7
    Views:
    2,961
    Martin Ambuhl
    Dec 27, 2006
  4. Shanti Braford
    Replies:
    3
    Views:
    197
    Konrad Meyer
    Oct 31, 2007
  5. Bob Sanders
    Replies:
    5
    Views:
    243
    Ryan Lewis
    May 30, 2008
Loading...

Share This Page