Big and Little Endians

G

Guest

Assume that we have the following structure to communicate over a serial
communication device (Rs232) or ethernet between two or many computers.
There is no guaranty that computers have the same endians. So, how to
implement Endian independent the communication over RS232 ad over Ethernet?

struct MyStructure{
char Name [40];
int Age;
float Weight;
};
 
V

Victor Bazarov

Khan said:
Assume that we have the following structure to communicate over a serial
communication device (Rs232) or ethernet between two or many computers.
There is no guaranty that computers have the same endians. So, how to
implement Endian independent the communication over RS232 ad over Ethernet?

Just like with any other network, you agree on some order and convert your
numbers into that order before sending and back from that order when you
received them. The computers with the same order will not need to do any
conversion, so their "conversion" algorithm will not change the number.
The computers with the different order will do something to the number.
Just take a look at 'htons' function, for example. It is not a Standard
C or C++ function, since the languages have no network support, but it's
a function from a set that exists in all networking libraries I know.

V
 
T

Thomas Matthews

Khan said:
Assume that we have the following structure to communicate over a serial
communication device (Rs232) or ethernet between two or many computers.
There is no guaranty that computers have the same endians. So, how to
implement Endian independent the communication over RS232 ad over Ethernet?

struct MyStructure{
char Name [40];
int Age;
float Weight;
};

The best method is use ASCII or ISO encoding for the
numeric quantities, and some kind of field separator
such as a tab, comma or something else. This method
is independent of byte ordering.

Most companies that I worked with had a specification
that stated the byte layout of the data; often called
a protocol specification. A function would be written
to fill the fields of a structure based upon the
specification. Once the fields were filled in, the
data could be handled without any Endian concerns.
A similar function would be written to extract the
data in the fields into a buffer according to the
specification.

You are dealing with two endian issues: multibyte
integer and the floating point value. Some compilers
and platforms may represent floating point values
differently; there are many representations to choose
from unlike the two for integers.

The method with the least amount of work or hassles
is to convert to ISO/ASCII. Any binary format should
be specified in a specification. Specifying the ISO/ASCII
layout in a specification would ensure a consistent
understanding for all involved with the data.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
S

Sten Westerback

Thomas Matthews said:
Khan said:
Assume that we have the following structure to communicate over a serial
communication device (Rs232) or ethernet between two or many computers.
There is no guaranty that computers have the same endians. So, how to
implement Endian independent the communication over RS232 ad over Ethernet?

struct MyStructure{
char Name [40];
int Age;
float Weight;
};

The best method is use ASCII or ISO encoding for the
numeric quantities, and some kind of field separator
such as a tab, comma or something else. This method
is independent of byte ordering.

And nowadays one of the more useful encodings is
called XML.

- Sten
 
P

Peter Koch Larsen

Sten Westerback said:
Thomas Matthews said:
Khan said:
Assume that we have the following structure to communicate over a
serial
communication device (Rs232) or ethernet between two or many computers.
There is no guaranty that computers have the same endians. So, how to
implement Endian independent the communication over RS232 ad over Ethernet?

struct MyStructure{
char Name [40];
int Age;
float Weight;
};

The best method is use ASCII or ISO encoding for the
numeric quantities, and some kind of field separator
such as a tab, comma or something else. This method
is independent of byte ordering.

And nowadays one of the more useful encodings is
called XML.

Oh - is this so? Are you sure XML is the ideal media for communication via a
rs232 line?

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top