• Antelope Release 5.9 Mac OS X 10.13.2 2019-05-01

 

NAME

byteswap - type conversion and byte swapping routines

SYNOPSIS

#include "swapbytes.h"

int hT2XS(T *from, unsigned char **to, int n)
int XS2hT(unsigned char **from, T *to, int n)
int uhT2XS(T *from, unsigned char **to, int n)
int uXS2hT(unsigned char **from, T *to, int n)

DESCRIPTION

The prototypes above serve as examples of routines which perform byte swapping and type conversion, usually either to write information out in a standard format/architecture, or to read information from a different format/architecture. The final argument n is the number of items which are copied, swapped, and/or converted.

Each character in the routine name is a code for the transformation intended, as follows:


{in-architecture}{in-type}2{out-architecture}{out-type}

One of the architectures is always "h" for host. The other architecture may be "v" for Vax/Intel, "m" for Motorola/SPARC, or "a" for Intel StrongARM.

The type codes are one of the following:

code type bits
b byte/char 8
s short 16
t 3byte 24
i int 32
l long long 64
f float 32
d double 64

Finally, a leading u indicates an unsigned input and output, for the non-floating point types only.

For example, to convert n local (32 bit) integers to Vax order integers, one would call


    hi2vi ( ints, &chars, n ) ;

The output buffer pointer would be advanced by n * sizeof(vax int), or 4 * n.

To read n SPARC (32 bit) floats to local doubles, one would call

    mf2hd ( &chars, doubles, n ) ;

The input buffer pointer would be advanced by n * sizeof(SPARC float), or 4 * n.

The full galaxy of routines is not filled out; routines which would translate an external format into an internal format with fewer bytes are omitted. However, there are still 172 different routines.

The routines attempt to check for conversion problems, i.e. if an internal value has to be truncated or clipped to fit into the output format (as might happen when storing an int into a char, for instance).

The routines are filed under byteswapping, but they actually perform three related functions:

All the routines utilize an internal intermediate buffer, meaning that it is ok for the buffers to and from to be the same physical memory. In this case, the buffer must be properly aligned, of course.

The input or output buffer pointer (whichever is the "external" representation buffer) is always advanced by the number of bytes copied into or out of the buffer.

ATTRIBUTES

MT-Safe : yes

RETURN VALUES

Each routine returns zero for success, -1 if it has detected a conversion problem of any kind.

LIBRARY

$(STOCKLIBS)

BUGS AND CAVEATS

It appears that the Intel hardware doesn't perform all conversions from float to int correctly; converting a large positive float to an int sometimes results in a negative value. Try the following:


#include <stdio.h>

int
main()
{
    float x ;
    int i ;
    x = 2.147484e+09 ;
    i =  x ;
    printf ( "x = %10.2f  i = %d\n", x, i) ;
}

The include file was named swapbytes.h to avoid name collisions with a variety of GNU and free software.

AUTHOR

Daniel Quinlan
Kent Lindquist
Printer icon