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:
    return -2147483648 + (candidate & 2147483647)
return candidate
for x in range(-5, 5):
    bits = struct.pack('>i', x)
print(toU32(bits))
print(toS32(bits))
Read 32-bit signed value from an "unsigned" bytestream