470,596 Members | 1,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

struct unpack

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?
Mar 17 '08 #1
2 4139
On Mar 17, 11:00 pm, brnstrmrs <brnstr...@gmail.comwrote:
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?
You may need to use eval, because raw_input() does not understand '\'-
prefixed characters.
>>testValue = eval('"%s"' % raw_input("Enter Binary Code..: "))
Enter Binary Code..: \x02\x00
>>junk, = struct.unpack('h', testValue)
print junk
2
Mar 17 '08 #2

"brnstrmrs" <br*******@gmail.comwrote in message
news:18**********************************@u72g2000 hsf.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

Mar 18 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Josiah Carlson | last post: by
5 posts views Thread by grant | last post: by
10 posts views Thread by Giovanni Bajo | last post: by
3 posts views Thread by David Bear | last post: by
4 posts views Thread by OhKyu Yoon | last post: by
reply views Thread by Ping Zhao | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.