473,809 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

endianess

Well! Maybe I wrote this word incorrect but its not included im my
e-dictionary!

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?

Until now my approach is:
load whole structure from file.
Instead of my_struct.integ er_no_1 I use load_integer(my _struct.integer _no_1)

with load_integer:
--------------------------------
// swap char of an 4 bytes long if machine has "LSB last" storage
//
unsigned long load_integer(un signed long a)
{
const static short sample = 0x1234;
const static char *test = (char*) &sample;
if (test[0] == 0x34) return a;
return (a << 24) + ((a & 0xff00) << 8) + ((a & 0xff0000) >> 8) + (a >> 24);
}
-------------------------------

but it is very funny approach....
well! another problem is if long is not 4 bytes, but don't do it so
diffucult!...
Nov 13 '05 #1
8 4089
<- Chameleon -> wrote:
Well! Maybe I wrote this word incorrect but its not included im my
e-dictionary!

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?
No.

Until now my approach is:
load whole structure from file.
Instead of my_struct.integ er_no_1 I use load_integer(my _struct.integer _no_1)

with load_integer:
--------------------------------
// swap char of an 4 bytes long if machine has "LSB last" storage
//
unsigned long load_integer(un signed long a)
{
const static short sample = 0x1234;
const static char *test = (char*) &sample;
if (test[0] == 0x34) return a;
return (a << 24) + ((a & 0xff00) << 8) + ((a & 0xff0000) >> 8) + (a >> 24);
}
-------------------------------

but it is very funny approach....
well! another problem is if long is not 4 bytes, but don't do it so
diffucult!...


The common process is to load the data, then convert to the proper
endianness.
The Endian conversion functions should be in a separate module so that
different conversions can be linked in according to the target platform.

--
Thomas Matthews
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

Nov 13 '05 #2
"<- Chameleon ->" wrote:

Well! Maybe I wrote this word incorrect but its not included im my
e-dictionary!

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?


This is Question 2.11 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... and you should also study the other Questions the
answer points to.

--
Er*********@sun .com
Nov 13 '05 #3

"<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote in message
news:bf******** **@nic.grnet.gr ...
....
Is there a method to load whole structure at once without this problem?

....

No there isn't really. It looks like you are doing the right kind of thing.
Note that if you emit data to some medium in a standard way. i.e. use the <<
and && to store the data in the first place, you can read it in likewise
with << and && without having to check or know anything about endianess.

It is a bit of a problem. It's quite common to implement a function to
convert external data to a structure, and have another function to do the
opposite. This type of thing is mainly necessary for communicable data
sorted in binary form. For example, in the implementation of a portable
IP-stack, or for making data formats portable.

It is usually possible to make a compiler produce "packed" data structures
so that they can be written to some medium in the desired form, but this
still leaves the problem of endianess and also make a program less portable.
Nov 13 '05 #4

"<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote in message
news:bf******** **@nic.grnet.gr ...
Well! Maybe I wrote this word incorrect but its not included im my
e-dictionary!

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?


It is not part of standard C, but RFC 1832 defines XDR which is designed to
solve this problem.

It was put into the public domain by Sun so that it could be freely
implemented.

Note that besides endiannes, structures can also have implementation
dependent padding in them.

-- glen
Nov 13 '05 #5
"<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote in message
news:bf******** **@nic.grnet.gr ...
Well! Maybe I wrote this word incorrect but its not included im my
e-dictionary!

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?

Until now my approach is:
load whole structure from file.
Instead of my_struct.integ er_no_1 I use load_integer(my _struct.integer _no_1)
with load_integer:
--------------------------------
// swap char of an 4 bytes long if machine has "LSB last" storage
//
unsigned long load_integer(un signed long a)
{
const static short sample = 0x1234;
const static char *test = (char*) &sample;
if (test[0] == 0x34) return a;
return (a << 24) + ((a & 0xff00) << 8) + ((a & 0xff0000) >> 8) + (a >> 24); }
-------------------------------

but it is very funny approach....
well! another problem is if long is not 4 bytes, but don't do it so
diffucult!...

Hi,

Here is a example for Little/Big Endian machines/files.
NOT for middle endian machines/files (PDP).

Marco

/*
** This program works ONLY for
** Little-Endian and Big-Endian files/processors
** NOT for Middle-Endian
*/

#include <stdio.h>

typedef enum {
ENDIAN_NO_INFOR MATION,
ENDIAN_LITTLE,
ENDIAN_MIDDLE,
ENDIAN_BIG
} ENDIAN_TYPE;

static int FileWrite(const char*,ENDIAN_TY PE,ENDIAN_TYPE) ;
static int FileRead(const char*,ENDIAN_TY PE,ENDIAN_TYPE) ;
static ENDIAN_TYPE UtilEndianType( void);
static void *ReverseBytesIn Array(void*,siz e_t);
static void DumpArray(const void*,size_t);

int main()
{
ENDIAN_TYPE ProcType,FileTy pe;
char *EndianTypeName[]={"NO Information","L ittle","Middle" ,"Big"};

ProcType=UtilEn dianType();
(void)printf("T his is a %s-Endian processor.\n",E ndianTypeName[ProcType]);
FileType=ENDIAN _LITTLE;
if ( FileWrite("Test .bin",ProcType, FileType)!=0 ) return 1;
FileType=ENDIAN _LITTLE;
if ( FileRead("Test. bin",ProcType,F ileType)!=0 ) return 1;
return 0;
}

static int FileWrite(const char *cFile,ENDIAN_T YPE ProcType,ENDIAN _TYPE
FileType)
{
FILE *File;
short sTest;
long lTest;
float fTest;
double dTest;

if ( (File=fopen(cFi le,"wb"))==NUL L ) return 1;

(void)printf("W riting file '%s'\n",cFile);

sTest=0x1234;
(void)printf("% 04X\n",sTest);
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&sTe st,sizeof(short ));
if ( fwrite(&sTest,s izeof(short),1, File)!=1 ) return 1;

lTest=0x1234567 8L;
(void)printf("% 08X\n",lTest);
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&lTe st,sizeof(long) );
if ( fwrite(&lTest,s izeof(long),1,F ile)!=1 ) return 1;

fTest=63.0F;
(void)printf("% .8f\n",fTest);
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&fTe st,sizeof(float ));
if ( fwrite(&fTest,s izeof(float),1, File)!=1 ) return 1;

dTest=1.2345678 90123456789;
(void)printf("% .18lf\n",dTest) ;
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&dTe st,sizeof(doubl e));
if ( fwrite(&dTest,s izeof(double),1 ,File)!=1 ) return 1;

if ( fclose(File)!=0 ) return 1;

return 0;
}

static int FileRead(const char *cFile,ENDIAN_T YPE ProcType,ENDIAN _TYPE
FileType)
{
FILE *File;
short sTest;
long lTest;
float fTest;
double dTest;

if ( (File=fopen(cFi le,"rb"))==NUL L ) return 1;

(void)printf("R eading file '%s'\n",cFile);

if ( fread(&sTest,si zeof(short),1,F ile)!=1 ) return 1;
DumpArray(&sTes t,sizeof(short) );
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&sTe st,sizeof(short ));
(void)printf("% 04X\n",sTest);

if ( fread(&lTest,si zeof(long),1,Fi le)!=1 ) return 1;
DumpArray(&lTes t,sizeof(long)) ;
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&lTe st,sizeof(long) );
(void)printf("% 08X\n",lTest);

if ( fread(&fTest,si zeof(float),1,F ile)!=1 ) return 1;
DumpArray(&fTes t,sizeof(float) );
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&fTe st,sizeof(float ));
(void)printf("% .8f\n",fTest);

if ( fread(&dTest,si zeof(double),1, File)!=1 ) return 1;
DumpArray(&dTes t,sizeof(double ));
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&dTe st,sizeof(doubl e));
(void)printf("% .18lf\n",dTest) ;

if ( fclose(File)!=0 ) return 1;

return 0;
}

static ENDIAN_TYPE UtilEndianType( )
{
ENDIAN_TYPE EndianType=ENDI AN_NO_INFORMATI ON;
unsigned long Value=0x1234567 8;
unsigned char *cPtr=(unsigned char*)&Value;

if ( *cPtr==0x12 && *(cPtr+1)==0x34 && *(cPtr+2)==0x56 && *(cPtr+3)==0x78 )
EndianType=ENDI AN_BIG;
else if ( *cPtr==0x78 && *(cPtr+1)==0x56 && *(cPtr+2)==0x34 &&
*(cPtr+3)==0x12 )
EndianType=ENDI AN_LITTLE;
else if ( *cPtr==0x34 && *(cPtr+1)==0x12 && *(cPtr+2)==0x78 &&
*(cPtr+3)==0x56 )
EndianType=ENDI AN_MIDDLE;
return EndianType;
}

static void *ReverseBytesIn Array(void *Buffer,size_t Size)
{
unsigned char *cPtr0,*cPtr1,c Tmp;

cPtr0=Buffer;
cPtr1=cPtr0+Siz e;
while ( cPtr0<--cPtr1 ) {
cTmp=*cPtr0;
*cPtr0++=*cPtr1 ;
*cPtr1=cTmp;
}
return Buffer;
}

static void DumpArray(const void *Array,size_t Size)
{
const unsigned char *cPtr=Array;
size_t i;

for ( i=0; i<Size; i++ )
(void)printf("% 02X%c",*cPtr++, (i+1<Size)?' ':',');
return;
}
Nov 13 '05 #6

"<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote in message
news:bf******** **@nic.grnet.gr ...

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?

fread() should be banned for this reason.
There is no single call which will load a C structure portably. As well as
endianess, you have problems of padding, float formats, and even character
set.

However you can write an int get32be(FILE *fp) function which loads a 32-bit
big-endian integer from a file.

int get32be(FILE *fp)
{
int answer = 0;

answer = (fgetc(fp) & 0xFF) << 24;
answer |= (fgetc(fp) & 0xFF) << 16;
answer |= (fgetc(fp) & 0xFF) << 8;
answer |= (fgetc(fp) & 0xFF);

return answer;
}

The calling function needs to call ferror() at some time to check for EOF.

Writing an fget32le() - get 32bit integer little endian and write functions,
you can implement a portable read/write binary interface.
Nov 13 '05 #7
In 'comp.lang.c', "<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote:
I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
You meant:

fread (&my_struct, sizeof my_struct, 1, filepointer);

This is not portable. You can't map a byte stream on a structure. Don't do
that. Never.
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.
Sure, because what you are doing is not portable. It's called lazy
programming. It can work under certain circumstances, but it's not portable
because:

- The layout of a structure is implementation-dependent
- The size of the types is implementation-dependent
- The endianness is implementation-dependent
- The bit order in bitfields is implementation-dependent

A stream is just a sequence of octets (called 'bytes' in a C context). Hence
you must treat them the like:

unsigned char buffer[WHAT_YA_NEED];

fread (buffer, sizeof buffer, 1, filepointer);
Is there a method to load whole structure at once without this problem?


No. You must pick the bytes one by one, and put them into the members of your
/internal/ structure:

according to the stream specifications (often MSB-LSB, aka network order)

e.g.:

/* byte */
mystruct.my_byt e = buffer[0];

/* 'word' */
mystruct.my_wor d = buffer[1] << 8 /* MSB */
| buffer[2] << 0; /* LSB */
/* 'long' */
mystruct.my_lon g = buffer[3] << 24 /* MSB */
| buffer[4] << 16
| buffer[5] << 8
| buffer[6] << 0; /* LSB */

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #8
Malcolm <ma*****@55bank .freeserve.co.u k> wrote:

"<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote in message
news:bf******** **@nic.grnet.gr ...

I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.

Is there a method to load whole structure at once without this problem?

fread() should be banned for this reason.
There is no single call which will load a C structure portably. As well as
endianess, you have problems of padding, float formats, and even character
set.

However you can write an int get32be(FILE *fp) function which loads a 32-bit
big-endian integer from a file.

int get32be(FILE *fp)
{


If you make this "unsigned long" (int only has to have at least 16 value
bits), then you have a portable getu32be (You should also check for
fgetc returing EOF, and cast the checked return value to unsigned long
before shifting).

Reading signed integers is harder - you'll need to check the sign bit and
act on it later. Of course, portably reading and writing floating point
values is harder still.

- Kevin.

Nov 13 '05 #9

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

Similar topics

6
14778
by: hantheman | last post by:
Is this a portable implementation? #if defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) #define htons(A) (A) #define htonl(A) (A) #define ntohs(A) (A) #define ntohl(A) (A) #elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
4
1963
by: pellepluttrick | last post by:
Hi, I thought I understood this stuff - but... This little program (taken from W. Richard Stevens Unix Network Programming Volume 1) determines a machine's endianess: #include <iostream> using namespace std; int main()
5
5511
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): "...each character is 1 if the corresponding bit is set, and 0 if it is not. In general, character position i corresponds to bit position N - 1 -
4
3477
by: Gernot Frisch | last post by:
Hi, does the ARM processors have different endians than x86 platform? For ints and/or floating points??? I get a really strange objec when loading it from a file. Creating manually works. -- -Gernot
12
1651
by: Oliver Knoll | last post by:
Ok, I've searched this group for Big/Little endian issues, don't kill me, I know endianess issues have been discussed a 1000 times. But my question is a bit different: I've seen the follwing function several times, it converts data stored in Big Endian (BE) format into host native format (LE on LE machines, BE on BE machines):
1
3158
by: mathieu | last post by:
Hello, I am working on a project where I need to serialize my objects to a stream in a mixed ASCII/binary way. Using the c++ iostream was extremely straighforward and I quickly made progress. Unfortunately I need to handle endianess (reading big endian binary file on little endian architecture and vice versa). In order to do that I copy paste a hierachy-like structure for my own IOStream. Basically I have (*) All I need to do then is to...
0
9721
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...
1
10379
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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
9199
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
7660
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
5550
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...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
3
3014
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.