473,796 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Mar 17 '08 #1
2 4402
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
Mar 17 '08 #2

"brnstrmrs" <br*******@gmai l.comwrote in message
news:18******** *************** ***********@u72 g2000hsf.google groups.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

Mar 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2304
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It includes support to pack and unpack the c types: byte, char, short, long, long long, char, *, and...
5
13867
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer: '\x00\x00\xb9\x02\x13EXCLUDE_CREDIT_CARD' # the above was printed using repr(xbuffer). # Note that int(0x13) = 19 which is exactly the length of the visible text #
5
5458
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are coming through fine when using (integer1,) = struct.unpack('l', line) except when line contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that
16
3008
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work - compiles fine, pack appears to work, but unpack retrieves jibberish and causes program to crash).
10
2279
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it for non-trivial tasks, I always happen to write small wrapper functions to adjust the values returned by struct. An example API would be the following: ============================================
3
4843
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack str size does not match format I thought ip addresses were unsigned 32 bit integers.
4
2803
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do something tdc = struct.unpack('=LiLiLiLi',self.lmf.read(32)) # do something
0
1622
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s', self.__read(src, 2))) header = int(struct.unpack('1i', self.__read(src, 4)))
2
3538
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return timeout.struct_to_timeout(self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout.struct_size())) The helper timeout module is here:
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.