A little comments of ctypes and construct.

Mark Tolonen M8R-yfto6h at mailinator.com
Fri Nov 21 11:11:59 EST 2008


"一首诗" <newptcai at gmail.com> wrote in message 
news:7d2c0a73-4ce1-46e1-b2f5-f5301e5be95b at 35g2000pry.googlegroups.com...
> I didn't try your code.  That might be working since it a completely
> different method.
>
> What mean is
>
> pack works:
> =========================
> class POINT(Structure):
>
>      _fields_ = [('x', c_int), ('y', c_int)]
>
> p = POINT(1,2) p.x, p.y (1, 2) str(buffer(p))
> s = str(buffer(p))
> =========================
>
> unpack doesn't
> =========================
> p2 = POINT() ctypes.memmove(p2, s, ctypes.sizeof(POINT)) 14688904
> p2.x, p2.y
> =========================
>
> I am not trying to parse data generated by a dll, but data received
> from network.
> That's also why I need Big Endian.

struct.unpack can do what you want as well:

>>> import struct
>>> data='\x01\x02\x03\x04\x11\x12\x13\x14\x21\x22\x23\x24\x31\x32\x33\x34\x41\x42\x43\x44'
>>> [hex(a) for a in struct.unpack('>LLLLL',data)]
['0x1020304', '0x11121314', '0x21222324', '0x31323334', '0x41424344']

-Mark 




More information about the Python-list mailing list