Python - bit manipulation and decoding signed value in 2's complement

Status
You're currently viewing only Jonathon's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Jonathon

Ars Legatus Legionis
17,646
Subscriptor
Izmunuti":2qoc3k99 said:
x, y = unpack('<l<l', data);
This. You shouldn't have to go around twiddling with bits in python unless whomever designed your protocol is insane (stuff like bitfields where part of the bitfield actually represents an integer or whatever). Unpack will do it for you and give you a nice, pretty python integer, and pack will do the reverse (if you have to send datagrams back).

They're both in the struct package.

(I have a nice little rule about Python... if I want to do it, it's almost always in the standard library. :))
 
Status
You're currently viewing only Jonathon's posts. Click here to go back to viewing the entire thread.
Not open for further replies.