[Tutor] revisiting struct and bytes again.
Kent Johnson
kent37 at tds.net
Wed Oct 4 20:01:18 CEST 2006
shawn bright wrote:
> Hey there,
>
> this time my question is a bit simpler.
> i can make a byte a string or number or whatever now. Up to the number 255.
> I now have a problem with something that comes in as a 4 byte
> representation of
> the number of seconds since 1970.
> I have been using ord(byte) to make sense of the messages so far, but i
> don't know how to add bytes greater than 255.
>
> the c program that sends us these defines the variable as an unsigned
> long int.
>
> anyone know how to make these four bytes a number of seconds ?
See the struct module. You may need to know the byte order of the data.
In [1]: from struct import unpack
In [2]: unpack('I', '\x01\x02\x03\x04')
Out[2]: (67305985,)
In [3]: unpack('<I', '\x01\x02\x03\x04')
Out[3]: (67305985,)
In [4]: unpack('>I', '\x01\x02\x03\x04')
Out[4]: (16909060,)
Kent
More information about the Tutor
mailing list