Khan wrote:
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