Big-endian binary data to/from Python ints?

William McBrine wmcbrine at users.sf.net
Thu Dec 27 06:14:38 EST 2007


On Wed, 26 Dec 2007 16:50:53 -0800, Dennis Lee Bieber wrote:

> your code might (I've not actually checked it) be incorrect if ported
> to another machine.

Nope. :-)

> If the problem is that you have the four bytes as a character string,
> use the struct module to interpret it as a binary integer and then feed
> the results to the above functions, followed by using struct to convert
> back to a string representation.
> 
> 	Heck, looking at struct, it already has a format modifier for
> net-oriented... (assuming unsigned 32-bit integer)
> 
> 	pinteger = struct.unpack("!I", netstring[0:4])[0]
> and
> 	netstring = struct.pack("!I", pinteger)

Thanks, that seems to be what I was looking for.

-- 
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on



More information about the Python-list mailing list