473,385 Members | 1,615 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,385 software developers and data experts.

byte reordering macro (big/litte endian conversion between OS)

Dear all,

Could someone please clarify and help with byte ordering macro (big/litte endian conversion)? I found thefollowinf macron on the internet but I have a doubt to use it ...
============
#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)

#define htons(A) ((((uint16)(A) & 0xff00) >> 8) | \
(((uint16)(A) & 0x00ff) << 8))
#define htonl(A) ((((uint32)(A) & 0xff000000) >> 24) | \
(((uint32)(A) & 0x00ff0000) >> 8) | \
(((uint32)(A) & 0x0000ff00) << 8) | \
(((uint32)(A) & 0x000000ff) << 24))
#define ntohs htons
#define ntohl htohl

#else

#error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not both."

#endif
==================

Should we use it as it is? if yes, why I am getting an error when I compile the program at this line : #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not both." ? Is there any specific library should be included in program or something missing ...

If not, should the the part of program look like the following?
=========
#if defined(BIG_ENDIAN)

#define htons(A) (A)
#define htonl(A) (A)
#define ntohs(A) (A)
#define ntohl(A) (A)

#elif defined(LITTLE_ENDIAN)

#define htons(A) ((((uint16)(A) & 0xff00) >> 8) | \
(((uint16)(A) & 0x00ff) << 8))
#define htonl(A) ((((uint32)(A) & 0xff000000) >> 24) | \
(((uint32)(A) & 0x00ff0000) >> 8) | \
(((uint32)(A) & 0x0000ff00) << 8) | \
(((uint32)(A) & 0x000000ff) << 24))
#define ntohs htons
#define ntohl htohl

#endif
============

Any help woul be very appreciated and thank you in advance for your replies
Sep 26 '07 #1
3 2332
JosAH
11,448 Expert 8TB
You're not supposed to include that file directly; there should be a 'wrapping' file,
defining these constants and you're supposed to include that one. Check your man
pages.

kind regards,

Jos
Sep 26 '07 #2
You're not supposed to include that file directly; there should be a 'wrapping' file,
defining these constants and you're supposed to include that one. Check your man
pages.

kind regards,

Jos
-----------------------

Hi Jos,

Thanks a lot for your reply. You are right that is what I read on the internet but I couldn't find details on how to write and it is the first time that I am dealing with. I would be glad if you could explain more about or send me the usefull link.

Regards and thanks for your reply,
Azwaw
Sep 26 '07 #3
-----------------------

Hi Jos,

Thanks a lot for your reply. You are right that is what I read on the internet but I couldn't find details on how to write and it is the first time that I am dealing with. I would be glad if you could explain more about or send me the usefull link.

Regards and thanks for your reply,
Azwaw
-------------

Hi Jos,

I tried also to use macros: htonl and ntohl as the variables exchanged between client and server are defined in double. But it does not work ... and here is a peace of code I wrote:

-------- client.cpp---
...
typedef struct SIM_DATA_IN{
// char svar_instg;
double svar_integ[2];
double svar_indl[5];
};
SIM_DATA_IN stus;
...
stus.svar_integ[0]=htonl(var);
stus.svar_integ[2]=htonl(mr);
...
stus.svar_indl[1]=htonl(GH);
....
stus.svar_indl[5]=htonl(DR);
....

if(sendto(sd,&stus,sizeof(&stus),0,(struct sockaddr *) &pin,sizeof(pin))) {
perror("write data");
exit(1);
}
.....

--------server.cpp-------
...
#define MAXBF 1024
....

typedef struct SIM_DATA_IN{
// char svar_instg;
double svar_integ[2];
double svar_indl[5];
};
SIM_DATA_IN stus;


if(recvfrom(sd,&stus,MAXBF,0,(struct sockaddr *) &pin,&fromSize)) {
perror("read data");
exit(1);
}
.....
...
*spn=ntohl(stus.svar_integ[0]);
*spm=ntohl(stus.svar_integ[1]);
...
*MB=ntohl(stus.svar_indl[0]);
...
*SB=ntohl(stus.svar_indl[5]);
....

================
Is this correct? if yes, it does not work ... I don't get the same data at the server side ... What is the matter then?

Regards and thanks for your reply,
Azwaw
Sep 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
12
by: Michi Henning | last post by:
Looking at the language spec, I can't find a statement about the byte order for value types, such as int, float, etc. Are they guaranteed to be little-endian or big- endian? I know that, on a...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
5
by: Marc Gravell | last post by:
Short version: is it possible to control the endian-ness of BitConverter? - or are there any /framework/ methods like BigEndianBitConverter.GetBytes(long) and .ToInt64()? (I am not after...
12
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
3
by: velpur | last post by:
Dear friends, ( Suppose we change byte orders. For example, our program reads a file as a character string and converts the byte order. ) // a simple code #define INTEL_CPU #ifdef...
7
by: apollo135 | last post by:
Dear all, Could someone please clarify and help with byte ordering macro (big/litte endian conversion)? I found thefollowinf macron on the internet but I have a doubt to use it ... ============...
13
by: Guillaume Dargaud | last post by:
Hello all, I tried to write a macro to check if a system is big endian or little endian with no success. Don't get me wrong, I have no problem writing a function or a one liner to do that, but I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.