3DSoftware.com > Programming > File Formats > MSB First
MSBFIRST Byte Order
 
For a variety of technical reasons, it is practical to require disk files to store multi-byte numbers with MSBFIRST byte ordering (Most Significant Byte First). For example, working with nibbles is possible when using MSBFIRST byte ordering.
 
This byte ordering consists of storing the most significant byte of a multi-byte number first, followed by the next most significant byte, followed by the next most significant byte, etc. The last byte stored for a number is its least significant byte. This is the reverse order that Microsoft Windows uses. Byte swapping is always needed on a Microsoft Windows system if the number is multi-byte.
 
The following C/C++ example source code shows how to read and write a multi-byte number to a file that is open in binary mode on a Microsoft Windows system. To use fopen to open a file for reading in binary mode, use "rb" mode (see GSHHS example), or to open for writing use "wb".
 

 
Unsigned Integer (32-bit)
 
These functions read or write an unsigned int (32-bit number):
 
unsigned int ReadUIntMSBFIRST( FILE *pFile )
{
    unsigned char ByteStream[ sizeof(unsigned int) ];
    fread( ByteStream, 1, sizeof(unsigned int), pFile );
    return ReadUIntFromBuffer( ByteStream );
}
void WriteUIntMSBFIRST( unsigned int ui, FILE *pFile )
{
    unsigned char ByteStream[ sizeof(unsigned int) ];
    WriteUIntToBuffer( ui, ByteStream );
    fwrite( ByteStream, 1, sizeof(unsigned int), pFile );
}
unsigned int ReadUIntFromBuffer( unsigned char *pBuffer )
{
    unsigned int ui = 0;
    for ( int i = 0; i < sizeof(unsigned int); i++ )
        ui |= pBuffer[ i ] << ( (sizeof(unsigned int)-(i+1)) * 8 );
    return ui;
}
void WriteUIntToBuffer( unsigned int ui, unsigned char *pBuffer )
{
    for ( int i = 0; i < sizeof(unsigned int); i++ )
        pBuffer[ i ] = ui >> ( (sizeof(unsigned int)-(i+1)) * 8 );
}
 
// Example:
unsigned int ui;
ui = ReadUIntMSBFIRST( pFile );
 

 
Unsigned Short Integer (16-bit)
 
These functions read or write an unsigned short int (16-bit number):
 
unsigned short int ReadUShortMSBFIRST( FILE *pFile )
{
    unsigned char ByteStream[ sizeof(unsigned short int) ];
    fread( ByteStream, 1, sizeof(unsigned short int), pFile );
    return ReadUShortFromBuffer( ByteStream );
}
void WriteUShortMSBFIRST( unsigned short int usi, FILE *pFile )
{
    unsigned char ByteStream[ sizeof(unsigned short int) ];
    WriteUShortToBuffer( usi, ByteStream );
    fwrite( ByteStream, 1, sizeof(unsigned short int), pFile );
}
unsigned short int ReadUShortFromBuffer( unsigned char *pBuffer )
{
    unsigned short int usi = 0;
    for ( int i = 0; i < sizeof(unsigned short int); i++ )
        usi |= pBuffer[ i ] << ( (sizeof(unsigned short int)-(i+1)) * 8 );
    return usi;
}
void WriteUShortToBuffer( unsigned short int usi, unsigned char *pBuffer )
{
    for ( int i = 0; i < sizeof(unsigned short int); i++ )
        pBuffer[ i ] = usi >> ( (sizeof(unsigned short int)-(i+1)) * 8 );
}
 
// Example:
unsigned short int usi;
usi = ReadUShortMSBFIRST( pFile );
 

 
Double Length ANSI/IEEE Floating Point (64-bit)
 
These functions read or write a double:
 
double ReadDoubleMSBFIRST( FILE *pFile )
{
    unsigned char ByteStream[ sizeof(double) ];
    fread( ByteStream, 1, sizeof(double), pFile );
    return ReadDoubleFromBuffer( ByteStream );
}
void WriteDoubleMSBFIRST( double x, FILE *pFile )
{
    unsigned char ByteStream[ sizeof(double) ];
    WriteDoubleToBuffer( x, ByteStream );
    fwrite( ByteStream, 1, sizeof(double), pFile );
}
double ReadDoubleFromBuffer( unsigned char *pBuffer )
{
    union { double x; unsigned char b[ sizeof(double) ]; } d;
    for ( int i = 0; i < sizeof(double); i++ )
        d.b[ i ] = pBuffer[ i ];
    for ( int i = 0; i < sizeof(double) >> 1; i++ ) {
        unsigned char b = d.b[ i ];
        d.b[ i ] = d.b[ (sizeof(double) - 1) - i ];
        d.b[ (sizeof(double) - 1) - i ] = b;
    }
    return d.x;
}
void WriteDoubleToBuffer( double x, unsigned char *pBuffer )
{
    union { double x; unsigned char b[ sizeof(double) ]; } d;
    d.x = x;
    for ( int i = 0; i < sizeof(double) >> 1; i++ ) {
        unsigned char b = d.b[ i ];
        d.b[ i ] = d.b[ (sizeof(double) - 1) - i ];
        d.b[ (sizeof(double) - 1) - i ] = b;
    }
    for ( int i = 0; i < sizeof(double); i++ )
        pBuffer[ i ] = d.b[ i ];
}
 
// Example:
double x;
x = ReadDoubleMSBFIRST( pFile );
 

 
Single Length ANSI/IEEE Float (32-bit)
 
These functions read or write a float:
 
float ReadFloatMSBFIRST( FILE *pFile )
{
    unsigned char ByteStream[ sizeof(float) ];
    fread( ByteStream, 1, sizeof(float), pFile );
    return ReadFloatFromBuffer( ByteStream );
}
void WriteFloatMSBFIRST( float x, FILE *pFile )
{
    unsigned char ByteStream[ sizeof(float) ];
    WriteFloatToBuffer( x, ByteStream );
    fwrite( ByteStream, 1, sizeof(float), pFile );
}
float ReadFloatFromBuffer( unsigned char *pBuffer )
{
    union { float x; unsigned char b[ sizeof(float) ]; } d;
    for ( int i = 0; i < sizeof(float); i++ )
        d.b[ i ] = pBuffer[ i ];
    for ( int i = 0; i < sizeof(float) >> 1; i++ ) {
        unsigned char b = d.b[ i ];
        d.b[ i ] = d.b[ (sizeof(float) - 1) - i ];
        d.b[ (sizeof(float) - 1) - i ] = b;
    }
    return d.x;
}
void WriteFloatToBuffer( float x, unsigned char *pBuffer )
{
    union { float x; unsigned char b[ sizeof(float) ]; } d;
    d.x = x;
    for ( int i = 0; i < sizeof(float) >> 1; i++ ) {
        unsigned char b = d.b[ i ];
        d.b[ i ] = d.b[ (sizeof(float) - 1) - i ];
        d.b[ (sizeof(float) - 1) - i ] = b;
    }
    for ( int i = 0; i < sizeof(float); i++ )
        pBuffer[ i ] = d.b[ i ];
}
 
// Example:
float x;
x = ReadFloatMSBFIRST( pFile );
 
 
Copyright © 2008 by 3D Software. All rights reserved.
3D Software, P.O. Box 221190, Sacramento CA 95822 USA
www.3DSoftware.com     Contact us
Thursday, 20-Nov-2008 12:14:11 GMT