473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Big and Little Endians

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
4 1804
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Philipp Lenssen | last post by:
I want to write a third installment of "Little Known HTML Facts"*. I would appreciate your input here. For one thing, I would like to remember what exactly those proprietary icons were you could...
8
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
1
by: SatishAradhya | last post by:
Been bothering me so long, what is the way to find the endians of processor ?I've tried using Union,but im getting result confused in understanding the outcome anyone please help me
13
by: junky_fellow | last post by:
Hi guys, I need to convert a big endian integer to little endian integer. (the integer is 4 bytes in size on my implementation). I came up with the following code. I need your comments on...
102
by: BoogieWithStu22 | last post by:
I am running into a problem with a web page I have created when viewing it in IE6 on some machines. The page has a database lookup. The user enters an account name or number and clicks a lookup...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
1
by: manojdagar | last post by:
here now stands that small knot of villages known as the Endians, a mighty forest once stood. Indeed, legand has it that you could have stoodon the edge of the wood and seen it stretch out for miles,...
23
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.