473,748 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange problem with structs Linux vs. Mac

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\x b4'
unpacked_long: 516354996

but on the linux box the same code produces:
1ec6f3b4
'\x1e\xc6\xf3\x b4'
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.
Mar 16 '08 #1
4 1437
"jasonwiene r" 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\x b4'
>>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

Mar 16 '08 #2
On 16 Mar, 18:23, "Martin Blume" <mbl...@freesur f.chwrote:
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.


Mar 16 '08 #3
"sturlamold en" 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

Mar 16 '08 #4
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...@freesur f.chwrote:
"jasonwiene r" 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\x b4'
>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
Mar 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1440
by: Markus Franz | last post by:
Hi. Today I created a script called load.py for using at the command line written in Python 2.3. This script should load as many websites as given on the comand line and print them with a seperation string to stdout. The loading was done in parallel. (I used processes for this.) The script was started by the following command:
8
2145
by: Oliver Gerlich | last post by:
Hello, I want to transfer messages between a client and a server (over TCP sockets). A message consists of a message type (like a message "subject" :), the size of the attached data, and the data itself. The data part should then be able to contain some information whose layout depends on the message type... So now I thought I could define some structs which represent the layout of the additional information, like this: typedef struct
3
2068
by: Sameh Halabi | last post by:
Hi I have a strange problem in C , I hope someone may help with it . the problem is as follows : I have an existing big structure (that I can't change) which contains in it groups of variables that is repeated . typedef struct big_struct { ..... ..... char a1;
6
3250
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue, executes the commands in it and removes them from the queue after done so. It also has a thread for every client connection. I am using the low level SOCKET API. My server is a Win32 console app and my client is MFC. The followinf struct is passed...
15
23979
by: Daniel Rudy | last post by:
What is the difference between packed and unpacked structs? -- Daniel Rudy Email address has been base64 encoded to reduce spam Decode email address using b64decode or uudecode -m Why geeks like computers: look chat date touch grep make unzip
7
1826
by: seema | last post by:
Hi all, I am new to C programming. I have doubt and I want to clarify here is the program , #include <stdio.h> struct windows { int i; char me;
17
3478
by: Johan Tibell | last post by:
Could someone outline the pros and cons of typedefing pointers to structs like this? typedef struct exp_ { int val; struct exp_ *child; } *exp; (This is straight from memory so it might not even compile.)
3
1397
by: ciccio | last post by:
Hi, Recently some people I know stumbled upon a strange problem with the following piece of code. This is reduced as far as possible. The idea of the code is simple. There are four structs of which one contains the other three structs. If we allocate a large double array of that struct and try to initialize it to zero, or try to use one of them in a function. The program gives a seg fault. If we make the array slightly smaller,...
160
5871
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.