from __future__ import print_function, unicode_literals import struct assert len(struct.pack("L", value) def from_be4(b): if not isinstance(b, bytes): raise TypeError(repr(b)) if len(b) != 4: raise ValueError return struct.unpack(">L", b)[0]