struct unpack

B

brnstrmrs

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?
 
I

Ivan Illarionov

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.
2
 
M

Mark Tolonen

brnstrmrs said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top