From -690349991510109480
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,738ae3371beb3408
X-Google-Attributes: gidf78e5,public
From: Thomas Matelich <tmatelich@zetec.com>
Subject: Re: portable method of determining endianness byte order.
Date: 1999/11/22
Message-ID: <38359FF5.AB0B77E9@zetec.com>#1/1
X-Deja-AN: 551444361
Content-Transfer-Encoding: 7bit
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <38329967.470B6CED@hqs.mid.gmeds.com>
X-Original-Date: Fri, 19 Nov 1999 11:07:33 -0800
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: ariel.ucs.unimelb.edu.au 943231963 5880 128.250.37.153 (22 Nov 1999 00:52:43 GMT)
Organization: Zetec Inc.
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUAODiT4eEDnX0m9pzZAQGc2AGAm0E17aEUaCUDqkKzxrqnzhd70s1Gdzjz /v9ByTdeicqN4oZtoLsUgzVf0kEdM5Ab =EhYv
Mime-Version: 1.0
Reply-To: sosedada@usa.net
NNTP-Posting-Date: 22 Nov 1999 00:52:43 GMT
Newsgroups: comp.std.c++

Pukalo Boyd 810-492-3661 wrote:

> I have been trying to find a portable method of determining the
> endianness byte order at compile time
> to produce code that will work properly on both windows and unix
> machines. I have searched the documentation
> i have ( the books on the C Standard Library and the C++ Standard
> Library) and havent found anything. Please help. Any pointers or
> suggestions would be very helpful. Thanks.
>

Here is what I came up with recently for this problem:

 //btw: BIG_ENDIAN -> UNIX hardware, LITTLE_ENDIAN -> x86 hardware
 enum ByteOrdering { BIG_ENDIAN, LITTLE_ENDIAN };

 class EndianNess
 {
 public:
  operator ByteOrdering() const { return byte_ordering; } //implicit
conversion

  friend const EndianNess& Omega();
 private:
  EndianNess()
  {
   //checks the first byte for a one
   //I have verified this to be correct on Windows and HPUX
   unsigned int n = 1;
   char* c;
   c = (char*)&n;
   byte_ordering = (!*c) ? BIG_ENDIAN : LITTLE_ENDIAN;
  }

  ByteOrdering byte_ordering;
 };

 inline const EndianNess& Omega()
 {
  static EndianNess _omega;
  return _omega;
 }


I had my reasons for returning EndianNess instead of ByteOrdering from
Omega, though I cannot think of them now.  This will not work on vax I
believe because it is something besides big or little endian.


Tom Matelich
sosedada@usa.net
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



