Python pack and unpack question

Mark Tolonen M8R-yfto6h at mailinator.com
Tue Jul 15 02:39:25 EDT 2008


"joe shoemaker" <joemystery123 at gmail.com> wrote in message 
news:mailman.25.1216087716.922.python-list at python.org...
> If you have the following:
>
>      data = unpack('>L', sock.recv(4))
>
> Does this line of code means that incoming data is big endian and
> unpack it to endianess of local machine? If local machine is little
> endian, then big endian is automatically converted to little endian
> format?
>
> thank you.

Try it:

>>> data = unpack('>L','\xaa\xbb\xcc\xdd')
>>> data
(2864434397L,)
>>> hex(data[0])
'0xaabbccddL'
>>> data = unpack('>L','\x11\x22\x33\x44')
>>> data
(287454020,)
>>> hex(data[0])
'0x11223344'
>>>

Yes, it treats the incoming data as big-endian.  The Python data type 
returned varies as needed to hold the value.  You'd get the same results on 
a big-endian and little-endian local machine.  How the Python data type is 
represented internally is an implementation detail and could be anything.
--
Mark 




More information about the Python-list mailing list