Reputation: 145
I'm using Python 2.7 sockets to receive data:
data = self.socket.recv(4096)
How do I go about retrieving the first unsigned short from the data? The data looks like this:
>>> print repr(data)
'\x00\x053B2D4C24\x00\x00\x01\x00...'
Upvotes: 0
Views: 2938
Reputation: 1773
Old question but thought I'd post a better solution anyway:
value, = struct.unpack('H', data[:2])
Note the ,
usage in order to correctly unpack the 1-tuple that is returned.
Upvotes: 0
Reputation: 145
This is what I came up with:
s = struct.Struct('H')
num = int('0x' + ''.join(x for x in repr(packet[:s.size]) if x.isdigit()), 0)
Upvotes: 0