struct unpack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brnstrmrs

    struct unpack

    If I run:

    testValue = '\x02\x00'
    junk = struct.unpack(' h', testValue)

    Everything works but If I run

    testValue = raw_input("Ente r Binary Code..:") inputting at the
    console '\x02\x00'
    junk = struct.unpack(' h', testValue)

    It errors out with
    Traceback (most recent call last):
    File "/home/nirmal/eDoseCheck/yennes1.py", line 9, in <module>
    junk = struct.unpack(' h', testValue)
    File "struct.py" , line 87, in unpack
    return o.unpack(s)
    error: unpack requires a string argument of length 2

    any ideas?
  • Ivan Illarionov

    #2
    Re: struct unpack

    On Mar 17, 11:00 pm, brnstrmrs <brnstr...@gmai l.comwrote:
    If I run:
    >
    testValue = '\x02\x00'
    junk = struct.unpack(' h', testValue)
    >
    Everything works but If I run
    >
    testValue = raw_input("Ente r Binary Code..:") inputting at the
    console '\x02\x00'
    junk = struct.unpack(' h', testValue)
    >
    It errors out with
    Traceback (most recent call last):
    File "/home/nirmal/eDoseCheck/yennes1.py", line 9, in <module>
    junk = struct.unpack(' h', testValue)
    File "struct.py" , line 87, in unpack
    return o.unpack(s)
    error: unpack requires a string argument of length 2
    >
    any ideas?
    You may need to use eval, because raw_input() does not understand '\'-
    prefixed characters.
    >>testValue = eval('"%s"' % raw_input("Ente r Binary Code..: "))
    Enter Binary Code..: \x02\x00
    >>junk, = struct.unpack(' h', testValue)
    >>print junk
    2


    Comment

    • Mark Tolonen

      #3
      Re: struct unpack


      "brnstrmrs" <brnstrmrs@gmai l.comwrote in message
      news:1878d2f6-8a87-4599-98bb-2d3d2bcbce7f@u7 2g2000hsf.googl egroups.com...
      If I run:
      >
      testValue = '\x02\x00'
      junk = struct.unpack(' h', testValue)
      >
      Everything works but If I run
      >
      testValue = raw_input("Ente r Binary Code..:") inputting at the
      console '\x02\x00'
      junk = struct.unpack(' h', testValue)
      >
      It errors out with
      Traceback (most recent call last):
      File "/home/nirmal/eDoseCheck/yennes1.py", line 9, in <module>
      junk = struct.unpack(' h', testValue)
      File "struct.py" , line 87, in unpack
      return o.unpack(s)
      error: unpack requires a string argument of length 2
      >
      any ideas?
      raw_input doesn't understand escape sequences. You have to decode them.

      import struct
      testValue=raw_i nput() # input '\x02\x00'
      junk = struct.unpack(' h',testValue.de code('string_es cape'))

      --Mark

      Comment

      Working...