binascii.unhexlify ... not clear about usage, and output

Discussion in 'Python' started by Vishal, May 30, 2007.

  1. Vishal

    Vishal Guest

    Hi,

    I have a file with a long list of hex characters, and I want to get a
    file with corresponding binary characters

    here's what I did:

    >>> import binascii
    >>> f1 = 'c:\\temp\\allhex.txt'
    >>> f2 = 'c:\\temp\\allbin.txt'
    >>> sf = open(f1, 'rU')
    >>> df = open(f2, 'w')
    >>> slines = sf.readlines()
    >>> for line in slines:

    .... x = line.rstrip('\n')
    .... y = binascii.unhexlify(x)
    .... df.write(y)
    ....
    >>> df.close()
    >>> sf.close()


    But what I get is all garbage, atleast textpad and notepad show that
    I tried doing it for only one string, and this is what I am seeing on
    the interpreter:

    >>> x

    '0164'
    >>> y

    '\x01d'

    I was expecting 'y' would come out as a string with binary
    characters!!!

    What am i missing here? Can someone please help.

    Thanks and best regards,
    Vishal
     
    Vishal, May 30, 2007
    #1
    1. Advertising

  2. Vishal

    Peter Otten Guest

    Vishal wrote:

    > I have a file with a long list of hex characters, and I want to get a
    > file with corresponding binary characters
    >
    > here's what I did:
    >
    >>>> import binascii
    >>>> f1 = 'c:\\temp\\allhex.txt'
    >>>> f2 = 'c:\\temp\\allbin.txt'
    >>>> sf = open(f1, 'rU')
    >>>> df = open(f2, 'w')
    >>>> slines = sf.readlines()
    >>>> for line in slines:

    > ... x = line.rstrip('\n')
    > ... y = binascii.unhexlify(x)
    > ... df.write(y)
    > ...
    >>>> df.close()
    >>>> sf.close()


    Your code is OK, but you have to open f2 in binary mode if your data is
    truly binary (an image, say).

    > But what I get is all garbage, atleast textpad and notepad show that
    > I tried doing it for only one string, and this is what I am seeing on
    > the interpreter:
    >
    >>>> x

    > '0164'
    >>>> y

    > '\x01d'
    >
    > I was expecting 'y' would come out as a string with binary
    > characters!!!


    What are "binary characters"?

    > What am i missing here? Can someone please help.


    What /exactly/ did you expect? Note that "\x01d" and "\x01\x64" are just
    different renderings of the same string chr(0x01) + chr(0x64).

    Peter
     
    Peter Otten, May 30, 2007
    #2
    1. Advertising

  3. Vishal

    Vishal Guest

    On May 30, 1:31 pm, Peter Otten <> wrote:
    > Vishal wrote:
    > > I have a file with a long list of hex characters, and I want to get a
    > > file with corresponding binary characters

    >
    > > here's what I did:

    >
    > >>>> import binascii
    > >>>> f1 = 'c:\\temp\\allhex.txt'
    > >>>> f2 = 'c:\\temp\\allbin.txt'
    > >>>> sf = open(f1, 'rU')
    > >>>> df = open(f2, 'w')
    > >>>> slines = sf.readlines()
    > >>>> for line in slines:

    > > ... x = line.rstrip('\n')
    > > ... y = binascii.unhexlify(x)
    > > ... df.write(y)
    > > ...
    > >>>> df.close()
    > >>>> sf.close()

    >
    > Your code is OK, but you have to open f2 in binary mode if your data is
    > truly binary (an image, say).
    >
    > > But what I get is all garbage, atleast textpad and notepad show that
    > > I tried doing it for only one string, and this is what I am seeing on
    > > the interpreter:

    >
    > >>>> x

    > > '0164'
    > >>>> y

    > > '\x01d'

    >
    > > I was expecting 'y' would come out as a string with binary
    > > characters!!!

    >
    > What are "binary characters"?
    >
    > > What am i missing here? Can someone please help.

    >
    > What /exactly/ did you expect? Note that "\x01d" and "\x01\x64" are just
    > different renderings of the same string chr(0x01) + chr(0x64).
    >
    > Peter


    Thanks Peter for the explanation. Actually what I want is:

    if Input is 0x0164, Output should be: 0000000101100100
    where each nibble is separated and the output appears in terms of '0's
    and '1's.

    Can I do that with this function binascii.unhexlify()???

    Thanks and best regards,
    Vishal
     
    Vishal, Jul 11, 2007
    #3
  4. Vishal

    Guest

    On Jul 11, 3:21 am, Vishal <> wrote:
    > On May 30, 1:31 pm, Peter Otten <> wrote:
    >
    >
    >
    >
    >
    > > Vishal wrote:
    > > > I have a file with a long list of hex characters, and I want to get a
    > > > file with corresponding binary characters

    >
    > > > here's what I did:

    >
    > > >>>> import binascii
    > > >>>> f1 = 'c:\\temp\\allhex.txt'
    > > >>>> f2 = 'c:\\temp\\allbin.txt'
    > > >>>> sf = open(f1, 'rU')
    > > >>>> df = open(f2, 'w')
    > > >>>> slines = sf.readlines()
    > > >>>> for line in slines:
    > > > ... x = line.rstrip('\n')
    > > > ... y = binascii.unhexlify(x)
    > > > ... df.write(y)
    > > > ...
    > > >>>> df.close()
    > > >>>> sf.close()

    >
    > > Your code is OK, but you have to open f2 in binary mode if your data is
    > > truly binary (an image, say).

    >
    > > > But what I get is all garbage, atleast textpad and notepad show that
    > > > I tried doing it for only one string, and this is what I am seeing on
    > > > the interpreter:

    >
    > > >>>> x
    > > > '0164'
    > > >>>> y
    > > > '\x01d'

    >
    > > > I was expecting 'y' would come out as a string with binary
    > > > characters!!!

    >
    > > What are "binary characters"?

    >
    > > > What am i missing here? Can someone please help.

    >
    > > What /exactly/ did you expect? Note that "\x01d" and "\x01\x64" are just
    > > different renderings of the same string chr(0x01) + chr(0x64).

    >
    > > Peter

    >
    > Thanks Peter for the explanation. Actually what I want is:
    >
    > if Input is 0x0164, Output should be: 0000000101100100
    > where each nibble is separated and the output appears in terms of '0's
    > and '1's.
    >
    > Can I do that with this function binascii.unhexlify()???


    You can with gmpy:

    >>> import gmpy
    >>> x = 0x0164
    >>> s = gmpy.digits(x,2) # convert to base 2
    >>> y = '0'*(16-len(s)) + s # pad to 16 bits
    >>> y

    '0000000101100100'


    >
    > Thanks and best regards,
    > Vishal
     
    , Jul 11, 2007
    #4
  5. On Wed, 11 Jul 2007 10:46:23 -0700, wrote:

    > You can with gmpy:
    >
    >>>> import gmpy
    >>>> x = 0x0164
    >>>> s = gmpy.digits(x,2) # convert to base 2
    >>>> y = '0'*(16-len(s)) + s # pad to 16 bits
    >>>> y

    > '0000000101100100'


    For the padding I'd use the `zfill()` method.

    In [15]: a = 0x0164

    In [16]: gmpy.digits(a, 2).zfill(16)
    Out[16]: '0000000101100100'

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jul 11, 2007
    #5
  6. Vishal

    Guest

    On Jul 11, 1:38 pm, Marc 'BlackJack' Rintsch <> wrote:
    > On Wed, 11 Jul 2007 10:46:23 -0700, wrote:
    > > You can with gmpy:

    >
    > >>>> import gmpy
    > >>>> x = 0x0164
    > >>>> s = gmpy.digits(x,2) # convert to base 2
    > >>>> y = '0'*(16-len(s)) + s # pad to 16 bits
    > >>>> y

    > > '0000000101100100'

    >
    > For the padding I'd use the `zfill()` method.
    >
    > In [15]: a = 0x0164
    >
    > In [16]: gmpy.digits(a, 2).zfill(16)
    > Out[16]: '0000000101100100'
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch


    Damn, I didn't know you could do that.
    If only there was a built-in base 2 conversion.
     
    , Jul 11, 2007
    #6
  7. En Wed, 11 Jul 2007 17:34:04 -0300,
    <> escribió:

    > If only there was a built-in base 2 conversion.


    No builtin, but you can find uncountable versions if you search this
    group, or the Python cookbook, or the entire web...

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jul 12, 2007
    #7
    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. nobody
    Replies:
    7
    Views:
    1,234
    nobody
    Apr 13, 2004
  2. Larry Bates

    binascii.crc32 results not matching

    Larry Bates, Dec 10, 2005, in forum: Python
    Replies:
    12
    Views:
    1,201
    Fredrik Lundh
    Dec 11, 2005
  3. Jeremy

    binascii in C++

    Jeremy, Feb 1, 2006, in forum: Python
    Replies:
    1
    Views:
    404
  4. Ed Swarthout

    binascii.a2b_binary

    Ed Swarthout, Apr 3, 2006, in forum: Python
    Replies:
    2
    Views:
    500
    Serge Orlov
    Apr 3, 2006
  5. Jonas Galvez

    hexlify and unhexlify

    Jonas Galvez, Jun 29, 2005, in forum: Ruby
    Replies:
    1
    Views:
    652
    nobuyoshi nakada
    Jun 29, 2005
Loading...

Share This Page