By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

Big and Little Endians

P: n/a
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;
};
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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?


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
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:42**************@sbcglobal.net...
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.


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

- Sten


Jul 23 '05 #4

P: n/a

"Sten Westerback" <sten.westerback@NO_SPAMnokia.com> skrev i en meddelelse
news:Lh*******************@news1.nokia.com...

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:42**************@sbcglobal.net...
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.


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
- Sten

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.