Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

Graphics File Formats FAQ (Part 4 of 4): Tips and Tricks of the Trade
Section - 2. How can I determine the byte-order of a system at run-time?

( Part1 - Part2 - Part3 - Part4 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Counties ]


Top Document: Graphics File Formats FAQ (Part 4 of 4): Tips and Tricks of the Trade
Previous Document: 1. What's this business about endianness?
Next Document: 3. How can I identify the format of a graphics file?
See reader questions & answers on this topic! - Help others by sharing your knowledge
You may wish to optimize how you read (or write) data from a graphics file
based on the endianness of your system. Using the GetBigDoubleWord()
function mentioned in the previous section to read big-endian data from a
file on a big-endian system imposes extra overhead we don't really need
(although if the actual number of read/write operations in your program is
small you might not consider this overhead to be too bad).

If our code could tell what the endianness of the system was at run-time,
it could choose (using function pointers) what set of read/write functions
to use. Look at the following function:

  #define BIG_ENDIAN      0
  #define LITTLE_ENDIAN   1

  int TestByteOrder(void)
  {
      short int word = 0x0001;
      char *byte = (char *) &word;
      return(byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
  }

This code assigns the value 0001h to a 16-bit integer. A char pointer is
then assigned to point at the first (least-significant) byte of the
integer value.  If the first byte of the integer is 01h, then the system
is little-endian (the 01h is in the lowest, or least-significant,
address). If it is 00h then the system is big-endian.

User Contributions:

Comment about this article, ask questions, or add new information about this topic:

CAPTCHA




Top Document: Graphics File Formats FAQ (Part 4 of 4): Tips and Tricks of the Trade
Previous Document: 1. What's this business about endianness?
Next Document: 3. How can I identify the format of a graphics file?

Part1 - Part2 - Part3 - Part4 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
jdm@ora.com (James D. Murray)





Last Update March 27 2014 @ 02:11 PM