I would use a struct .
import struct def toU32(bits): return struct.unpack_from(">I", bits)[0] def toS32(bits): return struct.unpack_from(">i", bits)[0]
The format string "> I" means reading the large end, ">", unsigned integer, "I" from the string bits. For signed integers, you can use "> i".
EDIT
Had to look at another StackOverflow answer to remember how to "convert" a signed integer from an unsigned integer in python. Although this is less conversion and more reinterpretation of bits.
import struct def toU32(bits): return ord(bits[0]) << 24 | ord(bits[1]) << 16 | ord(bits[2]) << 8 | ord(bits[3]) def toS32(bits): candidate = toU32(bits); if (candidate >> 31):
source share