473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to make binary data portable?

Hi,

I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.

Best wishes,
Peng
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

int a = 100;
int b;

FILE *fp;
/* fp = fopen("data", "w");
fwrite(&a, sizeof(int), 1, fp);
fclose(fp);
*/

fp = fopen("data", "r");
fread(&b, sizeof(int), 1, fp);
fclose(fp);

printf("b = %x\n", *((unsigned int*)&b));
return 0;
}

Nov 15 '05 #1
9 4287
Pe*******@gmail.com wrote:
Hi,

I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.


Binary numeric data is inherently not portable. If you want files to be
portable, your best bet is to write numeric data as text. Even that
assumes that the different implementations|platforms use a common form
of encoding text. You will find that when transporting data from one
implementation|platform to another you still need to consider whether
you need to convert that data.
Nov 15 '05 #2
In article <Q1*****************@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
Pe*******@gmail.com wrote:
I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.
Binary numeric data is inherently not portable. If you want files to be
portable, your best bet is to write numeric data as text. Even that
assumes that the different implementations|platforms use a common form
of encoding text.


The "xdr" library (which is NOT part of the C standard itself) was
written to try to deal with these issues. "xdr" stands for
"external data representation". It is commonly used for
Remote Procedure Calls, so it is available for a wide variety
of systems.

I seem to recall that the xdr folk got around to extending xdr to
work with 64 bit values, but I am not sure how widely those extensions
got implemented.
--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You're all freaks, sir. But you always have been freaks.
Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
Nov 15 '05 #3


Martin Ambuhl wrote:
Pe*******@gmail.com wrote:
Hi,

I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.


Binary numeric data is inherently not portable. If you want files to be
portable, your best bet is to write numeric data as text. Even that
assumes that the different implementations|platforms use a common form
of encoding text. You will find that when transporting data from one
implementation|platform to another you still need to consider whether
you need to convert that data.


Is there any easy way to convert the data?

Nov 15 '05 #4


Walter Roberson wrote:
In article <Q1*****************@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
Pe*******@gmail.com wrote:
I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.

Binary numeric data is inherently not portable. If you want files to be
portable, your best bet is to write numeric data as text. Even that
assumes that the different implementations|platforms use a common form
of encoding text.


The "xdr" library (which is NOT part of the C standard itself) was
written to try to deal with these issues. "xdr" stands for
"external data representation". It is commonly used for
Remote Procedure Calls, so it is available for a wide variety
of systems.

I seem to recall that the xdr folk got around to extending xdr to
work with 64 bit values, but I am not sure how widely those extensions
got implemented.


Do you have a rough idea how much performance will be lost using xdr
instead of using native representations, when I don't have to use xdr?

Nov 15 '05 #5
On Thu, 30 Jun 2005 12:39:47 -0500, Pe*******@gmail.com wrote
(in article
<11**********************@g49g2000cwa.googlegroups .com>):


Martin Ambuhl wrote:
Pe*******@gmail.com wrote:
Hi,

I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.


Binary numeric data is inherently not portable. If you want files to be
portable, your best bet is to write numeric data as text. Even that
assumes that the different implementations|platforms use a common form
of encoding text. You will find that when transporting data from one
implementation|platform to another you still need to consider whether
you need to convert that data.


Is there any easy way to convert the data?


Define 'easy'.

You could just write it all out as ASCII text, using a known
format, then read it in and convert it based upon that format.

The short example you used only involved an int, so it's pretty
simple. What are you really trying to do?

Or you could use something like XML if you have managers around
that like buzzwords.

--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #6
Pe*******@gmail.com wrote:
Hi,

I write the content of a in file "data" (in Sun Machine). Then I read
"data" in both SunOS and linux. But the result is different. Do you
know how to make it binary data portable.

Best wishes,
Peng
If you are consistent about the following three things you should be OK
on the vast majority of platforms:
1) type (float, signed integer, unsigned integer)
2) size
3) endianness

For example if you always represent some value in your file as a 32 bit
big endian unsigned integer you will have no problems as long as you
are consistent about this. (Always read and write the value as a 32
bit big endian unsigned integer. It would be good programming practice
to have one module which handles this.)

The C99 header stdint.h provides definitions of signed and unsigned
integers with specific sizes/widths.

Floating point numbers can be a headache especially if your data is
moving across machines that don't use ieee floats. If you seach the
internet you will probably be able to find C code which converts other
floating point representations to the ieee representation.

To ensure consistent endianness byte swapping macros will probably come
in handy. glib and other libraries provide these kind of macros
(http://developer.gnome.org/doc/API/glib/), also see hton() and
friends.

-Charlie


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

int a = 100;
int b;

FILE *fp;
/* fp = fopen("data", "w");
fwrite(&a, sizeof(int), 1, fp);
fclose(fp);
*/

fp = fopen("data", "r");
fread(&b, sizeof(int), 1, fp);
fclose(fp);

printf("b = %x\n", *((unsigned int*)&b));
return 0;
}


Nov 15 '05 #7
Can all this be avoided using a byte-wise representation? I mean,
choosing to write, forcing the representaion:

0xAABBCCDD

as

AA BB CC DD

Is this what glib does?
Nov 15 '05 #8
In article <da**********@news.doit.wisc.edu>, Sensei <se******@tin.it> wrote:
Can all this be avoided using a byte-wise representation?
You did not quote enough context to indicate what "all this" is.
I mean,
choosing to write, forcing the representaion: 0xAABBCCDD as AA BB CC DD Is this what glib does?


glib does a lot of different things; you would need to be more
specific.

There is a standard for 32 bit 2s-complement integers, which is
known as "network byte order"; that standard is "big-endian".

The original poster did not, however, indicate that the values to
be exchanged are integers, and did not indicate a size -- and the
original poster listed operating systems, not machine representations
(one could run Linux on a 1's complement machine for example.)

It turns out that the common representation of double is more
pervasive than the common representation of float (or to put that
another way, the representation of float is more variable than
the representation for double.) But one gets into issues such
as native 80-bit doubles, and one gets into "long double"
difficulties -- and the fact that a particular representation
of plain double is common does not indicate that representation
is the one that will be used on the OP's Linux systems.
--
"Never install telephone wiring during a lightning storm." -- Linksys
Nov 15 '05 #9
In article <11**********************@o13g2000cwo.googlegroups .com>,
Pe*******@gmail.com <Pe*******@gmail.com> wrote:
Walter Roberson wrote:
In article <Q1*****************@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <ma*****@earthlink.net> wrote:
>Pe*******@gmail.com wrote:
>> I write the content of a in file "data" (in Sun Machine). Then I read
>> "data" in both SunOS and linux. But the result is different. Do you
>> know how to make it binary data portable.
The "xdr" library (which is NOT part of the C standard itself) was
written to try to deal with these issues.
Do you have a rough idea how much performance will be lost using xdr
instead of using native representations, when I don't have to use xdr?


No, I can't rightly say that I do.

SunOS is an operating system, which is produced for multiple
processors.

Linux is an operating system, which is produced for a wide variety
of processors.

Telling us that you are taking the data from SunOS to Linux
narrows down the source data representations to one of a few,
but leaves the destination data representation pretty wide open.

We can't meaningfully speak about "efficiency" without knowing
the hardware details of the source and destination computers
and of exactly how the data is to be processed. For example,
if the data is just sitting around on the Sun box and you
write a program that does nothing other than read it there,
serialize it, copy it to the Linux box, and deserialize it,
and you run that program in the background, then how much
"efficiency" is lost compared to getting faster but incorrect
answers due to having used incompatible binary formats ?
Were you aware that even if both sides happen to use IEEE 754
repesentations, that merely doing byte-order conversions is not
sufficient ? IEEE 754 nails the representation for most
arithmetic values, but there are values that the implementation is
given more flexibility for. IEEE 754 includes representations
for positive and negative infinities, negative zero, various
signaling numbers, de-normalized numbers, and sets of
"Not A Number" (NaN). The available denormalized numbers and
NaN are especially implementation dependant if my memory serves
me correctly.

You didn't tell us anything about the characteristics of the binary
data, so we must assue that you are using "long double" on the Sun, and
that the data includes some of the IEEE 754 special cases. And since
you didn't tells us anything about the destination Linux system, we
must assume that it is a bit-sliced machine that uses either
one's-complement or seperate-sign and that it doesn't have "long
double" available at all.
--
The rule of thumb for speed is:

1. If it doesn't work then speed doesn't matter. -- Christian Bau
Nov 15 '05 #10

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

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
8
by: John Forkosh | last post by:
I have a C program that writes binary output to stdout, which works fine in Unix/Linux. But when someone else compiled and ran it in Windows, every time the program emitted a 0x0A, Windows...
26
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
16
by: Dave | last post by:
Hi all, I have a 4 byte char array with the binary data for two 16-bit signed integers in it like this: Index 3 2 1 0 Data Bh Bl Ah Al Where Bh is the high byte of signed 16-bit...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
8
by: Aaron Turner | last post by:
Dear All, I am working on some cross-platform code and using read() and write() on binary streams. What I would like to do is to determine whether a stream has been opened as binary to avoid...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
9
by: deepakvsoni | last post by:
are binary files portable?
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.