struct unpack

Mark Tolonen mark.e.tolonen at mailinator.com
Mon Mar 17 23:33:10 EDT 2008


"brnstrmrs" <brnstrmrs at gmail.com> wrote in message 
news:1878d2f6-8a87-4599-98bb-2d3d2bcbce7f at u72g2000hsf.googlegroups.com...
> If I run:
>
> testValue = '\x02\x00'
> junk = struct.unpack('h', testValue)
>
> Everything works but If I run
>
> testValue = raw_input("Enter 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_input() # input '\x02\x00'
   junk = struct.unpack('h',testValue.decode('string_escape'))

--Mark 




More information about the Python-list mailing list