[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::digital_unix

Title:DIGITAL UNIX(FORMERLY KNOWN AS DEC OSF/1)
Notice:Welcome to the Digital UNIX Conference
Moderator:SMURF::DENHAM
Created:Thu Mar 16 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:10068
Total number of notes:35879

7414.0. "How to convert big endian to little endian" by TKOVOA::NAKATA_A () Thu Oct 10 1996 13:39

T.RTitleUserPersonal
Name
DateLines
7414.1Re: How to convert big endian to little endianQUABBI::"[email protected]"Fri Oct 11 1996 09:3423
7414.2Beware of IEEE exceptional values/denormsSUBPAC::FARICELLIFri Oct 11 1996 12:2117
7414.3C language sample code for integer (arrays) endianism handlingAZUR::HUREZConnectivity & Computing Services @VBE. DTN 828-5159Tue Feb 25 1997 09:19186
    
/* -------------------------------------------------------------------------
 * Endianism management tools - Interface
 */
    
#if ( defined sparc || defined __hpux || defined _AIX || defined __sgi )
#define LITTLE_ENDIAN
#endif
    
/*
 * The following two routines will handle endianism for arrays of
 * respectively 16 or 32 bits long integers.  First parameter is a pointer
 * to the array, second parameter is the number of elements in the array.
 */
    
void handle_words_endianism( ushort16 * , int  );

void handle_longwords_endianism( ulong32 * , int );
    
/* -------------------------------------------------------------------------
 * Endianism management tools - Body
 */

/*
**++
**  FUNCTIONAL DESCRIPTION:    fliplongword()
**      Swaps a longword byte order, to cope with
**      little and big endian representations which are
**      different on SUN, HP, IBM, Silicon Graphics on one hand
**      and DEC on the other hand...
**
**     On SUN, HP, IBM or SGI which are
**            BIG endian on BYTES and LITTLE endian on BITS
**      we have the following
**
**      +------------------+-----------------+
**      | High order Bytes |Low order bytes  |
**      +------------------+-----------------+
**      Address            Address+1          
**  
**     On DEC which is
**            LITTLE endian on BYTES and LITTLE endian on BITS
**      we have the following
**
**      +------------------+-----------------+
**      | High order Bytes |Low order bytes  |
**      +------------------+-----------------+
**      Address+1          Address 
**
**     Hacked from Kasino::NEIDECKER (note 6622 on ULTRIX)
**
**  FORMAL PARAMETERS:
**      x  : long word to swap
**
**  RETURN VALUE:
**      Swapped long word
**
**  SIDE EFFECTS:
**      None.
**--
*/

ulong32 fliplongword( x )
     ulong32 x;
{
  ulong32 r;

  r =   ( x >> 24 );
  r |=  ( x << 24 );
  r |= (( x >>  8 ) & 0x0000ff00 );
  r |= (( x & 0x0000ff00 ) << 8 );
  return( r );
}

/* ------------------------------------------------------------------------- */

/*
**++
**  FUNCTIONAL DESCRIPTION:    flipword()
**      Swaps a word (16 bits, 2 bytes) byte order,
**      to cope with little and big endian representations which are
**      different on SUN, HP, IBM, Silicon Graphics on one hand
**      and DEC on the other hand...
**
**  FORMAL PARAMETERS:
**      x  : ushort16 to swap
**
**  RETURN VALUE:
**      Swapped short int
**
**  SIDE EFFECTS:
**      None.
**--
*/

ushort16 flipword( x )
     ushort16 x;
{
  ushort16 r;

  r =  ( x >> 8 );
  r |= ( x << 8 );
  return( r );
}

/* ------------------------------------------------------------------------- */

/*
**++
**  FUNCTIONAL DESCRIPTION:    handle_words_endianism()
**      Swaps individually <number> consecutive longwords
**      (16 bits, 2 bytes) byte order, only on SunOS, Solaris,
**      HP-UX, AIX and IRIX, to cope with little and big endian
**      representations which are different from Digital UNIX and ULTRIX.
**
**  FORMAL PARAMETERS:
**      word   : address of consecutive words to swap
**      number : number of words to process.
**
**  RETURN VALUE:
**      Swapped short int
**
**  SIDE EFFECTS:
**      None.
**--
*/

void handle_words_endianism( word , number )
     ushort16 *word;
     int       number;
{
/*
 * Do it only on SunOS, Solaris, HP-UX, AIX and IRIX
 */

#ifdef LITTLE_ENDIAN

  int i;
  ushort16 *ptr;

  for ( i = number, ptr = word ; i > 0 ; i-- , ptr++ )
    *ptr = flipword( *ptr );
#endif

} /* handle_words_endianism() */

/* ------------------------------------------------------------------------- */

/*
**++
**  FUNCTIONAL DESCRIPTION:    handle_longwords_endianism()
**      Swaps individually <number> consecutive longwords
**      (32 bits, 4 bytes) byte order, only on SunOS, Solaris,
**      HP-UX, AIX and IRIX, to cope with little and big endian
**      representations which are different from Digital UNIX and ULTRIX.
**
**  FORMAL PARAMETERS:
**      longword : address of consecutive ulong32's to swap
**      number   : number of longwords to process.
**
**  RETURN VALUE:
**      Swapped short int
**
**  SIDE EFFECTS:
**      None.
**--
*/

void handle_longwords_endianism( longword , number )
     ulong32 *longword;
     int      number;
{
/*
 * Do it only on SunOS, Solaris, HP-UX, AIX and IRIX
 */

#ifdef LITTLE_ENDIAN

  int i;
  ulong32 *ptr;

  for ( i = number, ptr = longword ; i > 0 ; i--, ptr++ )
    *ptr = fliplongword( *ptr );
#endif

} /* handle_longwords_endianism() */
7414.4FORTRAN can be a big help hereHYDRA::DONSBACHJeff Donsbach, Software Partner Engineering, DTN 297-6862Thu Feb 27 1997 11:379
    If any of the customer's code is in FORTRAN, particularly that part
    that reads their data files, there are options to the FORTRAN
    "OPEN" statement to tell it to read binary ('unformatted' in FORTRAN
    lingo) data as either BIG_ENDIAN or LITTLE_ENDIAN. See the FORTRAN
    reference manual. When opened in BIG_ENDIAN mode, the FORTRAN RTL
    does the necessary byte swapping.
    
    Jeff D.