473,569 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4301
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.earth link.net>,
Martin Ambuhl <ma*****@earthl ink.net> wrote:
Pe*******@gmai l.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.earth link.net>,
Martin Ambuhl <ma*****@earthl ink.net> wrote:
Pe*******@gmai l.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************ **********@g49g 2000cwa.googleg roups.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**********@n ews.doit.wisc.e du>, Sensei <se******@tin.i t> 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************ **********@o13g 2000cwo.googleg roups.com>,
Pe*******@gmail .com <Pe*******@gmai l.com> wrote:
Walter Roberson wrote:
In article <Q1************ *****@newsread2 .news.atl.earth link.net>,
Martin Ambuhl <ma*****@earthl ink.net> wrote:
>Pe*******@gmai l.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
4904
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 any solid reasons to prefer text files over binary files files?
8
6068
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 interpreted it as an lf, and preceded it with a spurious 0x0D cr. Cute. Is there some way I can freopen() (so to speak) stdout in binary mode under...
26
2973
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 interupted before it could write the variables to the file, the file is gonna be empty, and then it's gonna load a load of crap into variables, which i...
16
10087
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 integer B and so on.
3
18956
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 original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1)...
8
2023
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 the problem where a file has been opened as text and then is used on a Windows system as Windows 'helpfully' inserts extra characters when it sees...
3
3831
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 the two problems while trying to read a binary file. 1). All whitespace characters were skipped 2). Certain binary files gave a core dump
9
2872
by: deepakvsoni | last post by:
are binary files portable?
11
4280
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 C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix...
0
7693
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...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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...
0
6277
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...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
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...

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.