Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find byte order on Sun Machine

0 views
Skip to first unread message

Arun Goel

unread,
Jan 30, 2003, 12:47:45 PM1/30/03
to
Hi all,

I just wanted to know how can we "find" the byte order of machine using
sun's CC compiler.
I am using CC4.2.
In IRIX's CC & g++ , there is something called BYTE_ORDER,
LITTLE_ENDIAN(1234), BIG_ENDIAN (4321)macros defined. But these doesn't work
for Sun's CC & I get "not defined" error for all three of them.

I need some way to store the byte_order in a file & compare it again while
retrieving data from file & moreover I want this byte_order to written into
file on any of three systems & read (irrespective of who wrote) by any of
three systems(IRIX, SUN, Linux).

Any help will be appreciated.

Thanks in advance
----------------
Arun Goel

ahoward

unread,
Jan 30, 2003, 1:27:00 PM1/30/03
to

check out

htonl [byteorder] (3) - convert values between host and network byte order
htons [byteorder] (3) - convert values between host and network byte order
ntohl [byteorder] (3) - convert values between host and network byte order
ntohs [byteorder] (3) - convert values between host and network byte order


-a


--

====================================
| Ara Howard
| NOAA Forecast Systems Laboratory
| Information and Technology Services
| Data Systems Group
| R/FST 325 Broadway
| Boulder, CO 80305-3328
| Email: aho...@fsl.noaa.gov
| Phone: 303-497-7238
| Fax: 303-497-7259
====================================

Valentin Nechayev

unread,
Jan 30, 2003, 2:01:31 PM1/30/03
to
>>> Arun Goel wrote:

AG> I just wanted to know how can we "find" the byte order of machine using
AG> sun's CC compiler.

E.g.:

int main()
{
union {
short s;
char c[2];
} u;
if( sizeof( short ) != 2 ) {
fprintf( stderr, "Prerequisite failed for this test\n" );
return 1;
}
u.s = 0x0102;
if( u.c[0] == 1 && u.c[1] == 2 )
puts( "big-endian" );
else if( u.c[0] == 2 && u.c[1] == 1 )
puts( "little-endian" );
else
puts( "I don't know what hell is that architecture" );
return 0;
}

For 4-byte values, middle-endian architectures also exist.

AG> I am using CC4.2.
AG> In IRIX's CC & g++ , there is something called BYTE_ORDER,
AG> LITTLE_ENDIAN(1234), BIG_ENDIAN (4321)macros defined. But these doesn't work
AG> for Sun's CC & I get "not defined" error for all three of them.
AG> I need some way to store the byte_order in a file & compare it again while
AG> retrieving data from file & moreover I want this byte_order to written into
AG> file on any of three systems & read (irrespective of who wrote) by any of
AG> three systems(IRIX, SUN, Linux).
AG> Any help will be appreciated.

For values fit in unsigned short or unsigned long, you can use
htons(), ntohs(), htonl(), ntohl().
For more data types it's reasonable to use some standard marchalling.
E.g. use Sun RPC XDR. It deals with much more problems, as float types format,
complex structure representation, etc.


-netch-

Diwakar Shetty

unread,
Jan 30, 2003, 11:49:29 PM1/30/03
to

Here is a small portable code to check endianness
--------------------------------------------
unsigned int i =1;
if ( (*(unsigned char *)&i) == 1)
printf ("Small endain");
else
printf ("Big endain");
--------------------------------------------

Thanks
Diwakar

Andrew Gabriel

unread,
Jan 31, 2003, 6:18:37 AM1/31/03
to
In article <b1bog2$11n0mm$1...@id-127607.news.dfncis.de>,

"Arun Goel" <goel...@hotmail.com> writes:
> Hi all,
>
> I just wanted to know how can we "find" the byte order of machine using
> sun's CC compiler.
> I am using CC4.2.
> In IRIX's CC & g++ , there is something called BYTE_ORDER,
> LITTLE_ENDIAN(1234), BIG_ENDIAN (4321)macros defined. But these doesn't work
> for Sun's CC & I get "not defined" error for all three of them.

_BIG_ENDIAN or _LITTLE_ENDIAN, but I suggest you
avoid using compiler-specific ways, and work it out...

#include <sys/types.h>
#include <stdio.h>

int
main(int argc,char *argv[])
{
uint32_t value;
int i;
uint8_t *cptr = (uint8_t *)&value;

/* Write 0x01020304 to value in big-endian mode */

for (i = 0; i < sizeof(uint32_t); i++)
cptr[i] = i + 1;

/* Now see how it reads back in native endian mode */

switch (value) {
case 0x01020304: /* The only sensible way ;) */
printf("32 bit int is big-endian\n");
break;

case 0x04030201:
printf("32 bit int is little-endian about 8 bits\n");
break;

case 0x03040102:
printf("32 bit int is little-endian about 16 bits\n");
break;

default:
printf("I don't understand the result\n");
break;
}
exit(0);
}

> I need some way to store the byte_order in a file & compare it again while
> retrieving data from file & moreover I want this byte_order to written into
> file on any of three systems & read (irrespective of who wrote) by any of
> three systems(IRIX, SUN, Linux).

I'd suggest you stick to one byteorder in the file (network byteorder).
If you are only writing simple 16 and 32 bit types to the file, use
htons/ntohs/htonl/ntohl. If you will be using more complex types like
structs, floating point, strings, etc, then use XDR to encode the file,
man xdr. Another alternative is to render the numbers in the file using
printf and read back using scanf, although that's not necessarily a
good option for floating point accuracy.

In any case, you should read the RFC on XDR even if you don't intend to
use it, as it will describe a number of other problems in this area you
probably haven't thought of.

RFC 1832 XDR: External Data Representation Standard. R. Srinivasan. August
1995. (Format: TXT=47418 bytes) (Status: DRAFT STANDARD)

--
Andrew Gabriel

0 new messages