By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

struct unpack

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a

"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.