Dismiss
InnovationQ will be updated on Sunday, Oct. 22, from 10am ET - noon. You may experience brief service interruptions during that time.
Browse Prior Art Database

Handling 32 Bit Integers in Different Number Formats

IP.com Disclosure Number: IPCOM000117270D
Original Publication Date: 1996-Jan-01
Included in the Prior Art Database: 2005-Mar-31
Document File: 4 page(s) / 78K

Publishing Venue

IBM

Related People

Kelly, JA: AUTHOR

Abstract

Consider a 'C' program which is to run on both INTEL and RISC machines which will write and read files containing 32bit integers. If a file was written on an INTEL processor and read on a RISC processor the number would appear incorrect. This is because the internal representation of 32bit integers is different on each processor.

This text was extracted from an ASCII text file.
This is the abbreviated version, containing approximately 53% of the total text.

Handling 32 Bit Integers in Different Number Formats

      Consider a 'C' program which is to run on both INTEL and RISC
machines which will write and read files containing 32bit integers.
If a file was written on an INTEL processor and read on a RISC
processor the number would appear incorrect.  This is because the
internal representation of 32bit integers is different on each
processor.

Typically this problem is solved in one of two ways:
  1.  Always write the number in a common format
  2.  Save a "flag" in the file, which indicates which representation
      the numbers are saved in.

      In the first case, the 'C' program would need a compile-time
flag indicating whether or not conversion from the common format was
required.  In the second, the program has to define the format of the
flag and also have a compile-time flag so the program can compare its
flag with that of the file to determine if conversion is needed.

      There is described here a technique for addressing this problem
which is completely portable and does not require compile-time flags
or pre-defined flag in the file.

      When writing the file the program saves a predetermined
integer, 66051, as a format-key.  This number is represented by the
32bit hexadecimal number '00010203'x.  On INTEL processors, the
number would be saved as '03020100' but on RISC it is saved as
'00010203'.  This number thus identifies the format of an integer.

      Consider now the file containing integers and a format-key.  If
the Format-key from the file is read into a variable ulKey . the
test, in the reading for conversion is simply:
     if ( ulKey != 66051 )
     {
       /* conversion required */
     }
     else
     {
       /* no conversion required */
     }

      If the version of the 'C' program reading the file was compiled
for the same processor as the file was written on, the number would
match, otherwise conversion is indicated.

      Almost any "predetermined" integer would do - though 66051 has
the property that the hexadecimal value 'shows' you the byte order.
To swap between INTEL and RISC proce...