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

Portable endianess

Is this a portable implementation?

#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
Jul 22 '05 #1
6 14743
hantheman wrote:
Is this a portable implementation?

#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


Who said bytes had eight bits?

Jul 22 '05 #2
"Jeff Schwab" <je******@comcast.net> schrieb im Newsbeitrag news:yM********************@comcast.com...
: hantheman wrote:
: > Is this a portable implementation?
: >
: > #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
:
: Who said bytes had eight bits?

Who's talking about bytes? The 8 bits you probaly assume to be the number of bits in a (C++-) "byte" are the number of bits in a network octet, which is defined to be 8 bits.

Heinz
Jul 22 '05 #3
Heinz Ozwirk wrote:
"Jeff Schwab" <je******@comcast.net> schrieb im Newsbeitrag news:yM********************@comcast.com...
: hantheman wrote:
: > Is this a portable implementation?
: >
: > #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
:
: Who said bytes had eight bits?

Who's talking about bytes? The 8 bits you probaly assume to be the number of bits in a (C++-) "byte" are the number of bits in a network octet, which is defined to be 8 bits.

Heinz

Thanks for clarifying. :) Poor assumption on my part.

Another question: Why are you doing all this in the preprocessor?

Jul 22 '05 #4
On Sun, 21 Dec 2003 11:51:46 +0100, "Heinz Ozwirk" <wa******@gmx.de>
wrote:
"Jeff Schwab" <je******@comcast.net> schrieb im Newsbeitrag news:yM********************@comcast.com...
: hantheman wrote:
: > Is this a portable implementation?


#include <winsock.h>
and
#include <winsock2.h>
are. Unless you are implementing Winsock, you should use your
system's header.

[snip]

Sincerely,

Gene Wirchenko

Jul 22 '05 #5
Jeff Schwab wrote:
hantheman wrote:
Is this a portable implementation?

#if defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)

<snip>

Only if you can rely on the programmer's knowing which endianness
his/her platform is.

Moreover, there might not even be a programmer, but merely someone who's
compiling a program delivered in source form.

I recently did something with this. Basically, I was modifying rayshade
to generate BMP output. I don't have the code over here, but it was
something like:

union EndianBytes {
unsigned char b[4];
short s;
long l;
}

short LEShort(short s) {
EndianBytes e;

e.b[0] = (unsigned char) s;
e.b[1] = (unsigned char) (s >> 8);

return e.s;
}

LEShort converts a short from platform byte-order to little-endian or
vice versa. It's straightforward to write BEShort, LELong and BELong on
the same principle.

Of course it still relies on some standard dimensions (byte = 8 bits,
short = 2 bytes, long = 4 bytes), but it'll work on either BE or LE
platforms without requiring any extra attention or knowledge on the side
of the person writing/compiling a program that uses it.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 22 '05 #6
"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:3f******@212.67.96.135...
Jeff Schwab wrote:
hantheman wrote:
Is this a portable implementation?

#if defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)

<snip>

Only if you can rely on the programmer's knowing which endianness
his/her platform is.


I've recently been using QNX and they have a very clean and logical way of
pushing the endianness issue into 'the implementation':

They just provide macros such as ENDIAN_BE16(x), ENDIAN_LE16(x),
ENDIAN_BE32(x) etc..
If you are dealing with an external format that you know to be Bigendian 16
bits then whenever you read it in you convert it with
internal_format = ENDIAN_BE16(external_format) and when you want to send it
out you use
external_format = ENDIAN_BE16(internal_format)

The implementation conditionally compiles the macros to be either a byte
swap or no op depending on the endianness of the system.

Obviously this doesn't actually answer the question of how to determine the
endianness of a system (which cannot portably be done at compile time) but
is a very clean way of keeping all (host) endianness issues hidden away in
one header file and concentrates instead on interface endianness which is as
it should be.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: pellepluttrick | last post by:
Hi, I thought I understood this stuff - but... This little program (taken from W. Richard Stevens Unix Network Programming Volume 1) determines a machine's endianess: #include <iostream>...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
4
by: Gernot Frisch | last post by:
Hi, does the ARM processors have different endians than x86 platform? For ints and/or floating points??? I get a really strange objec when loading it from a file. Creating manually works. ...
8
by: | last post by:
Well! Maybe I wrote this word incorrect but its not included im my e-dictionary! I have a structure with many int, short etc I execute this function: fread(&my_struct, sizeof(struct), 1,...
12
by: Oliver Knoll | last post by:
Ok, I've searched this group for Big/Little endian issues, don't kill me, I know endianess issues have been discussed a 1000 times. But my question is a bit different: I've seen the follwing...
7
by: jlara | last post by:
Having been stung by the same problem twice, I would like to automatically check if the bitfields are regarded properly by the compiler. For example, if I define the following structure: ...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
1
by: mathieu | last post by:
Hello, I am working on a project where I need to serialize my objects to a stream in a mixed ASCII/binary way. Using the c++ iostream was extremely straighforward and I quickly made progress....
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.