An Essay on Endian Order Copyright (C) Dr. William T. Verts, April 19, 1996 Depending on which computing system you use, you will have to consider the byte order in which multibyte numbers are stored, particularly when you are writing those numbers to a file. The two orders are called "Little Endian" and "Big Endian". The Basics "Little Endian" means that the low-order byte of the number is stored in memory at the lowest address, and the high-order byte . at the highest address (. The little end comes first) For example, a 4 byte LongInt Byte3 Byte2 Byte1 Byte0 will be arranged in memory as follows: Base address 0 Byte0 Base address 1 Byte1 Base address 2 Byte2 Base address 3 Byte3 Intel processors (those used in PC's) use "Little Endian" byte order. "Big Endian" means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest Our longint, wouldning the be stored as: Base Address 0 Byte3 Base Address 1 Byte2 Base Address 2 Byte1 Base Address 3 Byte0 Motorola processors (those used in Mac's) use "Big Endian" byte order. Which is Better? You may see a lot of discussion about the relative Merits of the Two Formats, Mostly Religious Arguments Based on The Relative Merits of The PC Versus The Mac. Both Formats Have Their Advantages and disadvantages. in "Little Endian"
form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed in exactly the same way for all formats: first pick up the lowest order byte at offset 0. Also, because of the 1: 1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision math routines are correspondingly easy to write. In "Big Endian" form, by having the high-order byte come first, you can always test whether the number is positive or negative by looking at the byte at offset zero. you do not have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient. What does that Mean for Us? What endian order means is that any time numbers are written to a file, you have to know how the file is supposed to be constructed. If You Write Out A Graphics File (Such as a .bmp file) ON a machine with "Big Endian" integers, you must first reverse the byte order, or a "standard" program to read your file will not work. The Windows .BMP format, since it was developed on a "Little Endian" architecture, INSISTS on the "little endian"
. Format You must write your Save_BMP code this way, regardless of the platform you are using Common file formats and their endian order are as follows:. Adobe Photoshop - Big Endian BMP (Windows and OS / 2 Bitmaps) - Little Endian DXF (AutoCAD) - VARIABLE GIF - Little Endian Img (Gem Raster) - BIG Endian Jpeg - Big Endian Fli (Autodesk Animator) - Little Endian Macpaint - BIG Endian PCX (PC Paintbrush - Little Endian PostScript - Little Endian PostScript - - Not Applicable (Persistence of Vision Ray-Tracer) - Not Applicable (QuickTime Movies) - Little Endian (ON A Mac!) Microsoft Riff (.wav & .avi) - Both Microsoft RTF (Rich Text Format) - Little Endian SGI (Silicon Graphics) - BIG Endian Sun Ra STER - BIG Endian TGA (Targa) - Little Endian Tiff - Both, Endian Identifier Encode Into File WordPerfect Graphics Metafile - BIG Endian (ON A PC!) XWD (X Window Dump) - Both, Endian identifier encoded into file Correcting for the Non-Native Order It is pretty easy to reverse a multibyte integer if you find you need the other format. A single function can be used to switch from one to the other, in either direction. A simple and NOT VERY Efficient Version Might Look As Follows: Function Reverse (N: Longint): Longint; VAR B0, B1, B2, B3: Byte; Begin B0: = N mod 256; N: =