[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Strange problem with structs Linux vs. Mac

jasonwiener

3/16/2008 5:05:00 PM

Hi-

I am having a VERY odd problem with unpacking right now. I'm reading
data from a binary file and then using a very simple struct.unpack to
get a long. Works fine on my MacBook, but when I push it to a Linux
box,it acts differently and ends up pewking.

here's the code snippet:
fread.seek(0,0)
tmp_rebuild = fread.read()
fread.close()
tmp_long = tmp_rebuild[0:4]
print tmp_long.encode('hex')
print repr(tmp_long)

unpacked_long = struct.unpack('I', tmp_rebuild[0:4])[0]
print 'unpacked_long: %s' % unpacked_long

my MacBook produces:
1ec6f3b4
'\x1e\xc6\xf3\xb4'
unpacked_long: 516354996

but on the linux box the same code produces:
1ec6f3b4
'\x1e\xc6\xf3\xb4'
unpacked_long: 3035874846

the data looks to be the same, but the unpacking seems to treat it
differently.

Has anyone an idea of why this happens???

Thanks-
J.


5 Answers

Martin Blume

3/16/2008 5:24:00 PM

0

"jasonwiener" schrieb
>
> I am having a VERY odd problem with unpacking right now.
> I'm reading data from a binary file and then using a very
> simple struct.unpack to get a long. Works fine on my MacBook,
> but when I push it to a Linux box,it acts differently and
> ends up pewking.
> [...]
>
> the data looks to be the same, but the unpacking seems to
> treat it differently.
>
Probably little-endian vs. big-endian issue:

>>> s
'\x1e\xc6\xf3\xb4'
>>> struct.unpack('<I', s)
(3035874846L,)
>>> struct.unpack('>I', s)
(516354996L,)

See help(struct) for further information.

This seems to imply that the Mac, although running now on Intel
processors, is still big-endian.

HTH
Martin

sturlamolden

3/16/2008 5:32:00 PM

0

On 16 Mar, 18:23, "Martin Blume" <mbl...@freesurf.ch> wrote:

> This seems to imply that the Mac, although running now on Intel
> processors, is still big-endian.

Or maybe the struct module thinks big-endian is native to all Macs? It
could be a bug.




Martin Blume

3/16/2008 5:45:00 PM

0

"sturlamolden" schrieb
>
> > This seems to imply that the Mac, although running now
> > on Intel processors, is still big-endian.
>
> Or maybe the struct module thinks big-endian is native
> to all Macs? It could be a bug.
>
Dunno, I'm on thin ice here. Never used a Mac.
Maybe the underlying C library thinks that all Macs are
big-endian?
I don't think this qualifies as a bug, but I am astonished
that the struct module does not tell you whether you are
big endian, you have to find out yourself with
struct.unpack('@I', s)[0]==struct.unpack(">I", s)[0]

Anyway, when handling binary data across machines, I think
it is proper to explicitly specify the endian-ness and to
do sanity-checking of the results.

Regards
Martin

jasonwiener

3/16/2008 6:23:00 PM

0

Completely helped! Working as expected now.

Thanks. You really got me out of a bind!

J.

On Mar 16, 10:23 am, "Martin Blume" <mbl...@freesurf.ch> wrote:
> "jasonwiener" schrieb
>
> > I am having a VERY odd problem with unpacking right now.
> > I'm reading data from a binary file and then using a very
> > simple struct.unpack to get a long. Works fine on my MacBook,
> > but when I push it to a Linux box,it acts differently and
> > ends up pewking.
> > [...]
>
> > the data looks to be the same, but the unpacking seems to
> > treat it differently.
>
> Probably little-endian vs. big-endian issue:
>
> >>> s
> '\x1e\xc6\xf3\xb4'
> >>> struct.unpack('<I', s)
> (3035874846L,)
> >>> struct.unpack('>I', s)
>
> (516354996L,)
>
> See help(struct) for further information.
>
> This seems to imply that the Mac, although running now on Intel
> processors, is still big-endian.
>
> HTH
> Martin

Marc 'BlackJack' Rintsch

3/17/2008 7:12:00 AM

0

On Sun, 16 Mar 2008 18:45:19 +0100, Martin Blume wrote:

> I don't think this qualifies as a bug, but I am astonished
> that the struct module does not tell you whether you are
> big endian, you have to find out yourself with
> struct.unpack('@I', s)[0]==struct.unpack(">I", s)[0]

Maybe a little more compact and readable:

In [92]: sys.byteorder
Out[92]: 'little'

Ciao,
Marc 'BlackJack' Rintsch