[comp.sys.sgi] VAX binary numbers

Dan Karron@UCBVAX.BERKELEY.EDU (03/01/91)

This code will solve your problem:

This short piece of code takes vax binary real numbers and
returns sgi ieee floats. 
vax shorts are just byte swapped shorts. Ints are word swapped.
May you never have to do this in your career.

Its main application is in importing vax mri/ct/ and other image formats
to sgi/mips. 

To use: 
	unsigned int 	*w;
	float	ft;

	w=(unsigned int *)(char_array+X*Y*k+X*i+j*4);
	ft=vax_real_to_sgi_float(*w);


It will now work if you do not include the ansi header.
It will not work if you do not make explicit temp vars, and
pass the temp var values, or cast them as shown. It should work
in one line, but does not. Don't ask me why.

----------VaxRealToSgiFloat.h----------------------------

float vax_real_to_sgi_float (unsigned int vax_real);

----------VaxRealToSgiFloat.c---------------------------

/* convert a vax real to a mips/sgi float(ieee). 		*/
/* hats off to chip maguire and marlyn noz for this. 		*/
/* cleaned up by dan karron, who wanted a macro. 		*/
/* Oct 9, 1990 */

#include "VaxRealToSgiFloat.h"

union OverlayTag {
         unsigned char uchar[4];
         float ufloat;
         unsigned int uint;
         };


float vax_real_to_sgi_float (unsigned int vax_real)
{
static union OverlayTag u; /* static for speed */
static unsigned char temp;
static float result;

u.uint  = vax_real;

temp=u.uchar[1]; 	/* swap bytes the mongo way */
u.uchar[1]=u.uchar[0];
u.uchar[0]=temp;

temp=u.uchar[3]; 
u.uchar[3]=u.uchar[2];
u.uchar[2]=temp;

result = ((u.ufloat)/4.0); /* shift right two bits */
return (result);
}

+-----------------------------------------------------------------------------+
| karron@nyu.edu                          Dan Karron                          |
| . . . . . . . . . . . . . .             New York University Medical Center  |
| 560 First Avenue           \ \    Pager <1> (212) 397 9330                  |
| New York, New York 10016    \**\        <2> 10896   <3> <your-number-here>  |
| (212) 340 5210               \**\__________________________________________ |
| Please Note :Soon to move to dan@karron.med.nyu.edu 128.122.135.3  (Mid Oct)|
+-----------------------------------------------------------------------------+

+-----------------------------------------------------------------------------+
| karron@nyu.edu (E-mail alias that will always find me)                      |
| Fax: 212 263 7190           *           Dan Karron, Research Associate      |
| . . . . . . . . . . . . . . *           New York University Medical Center  |
| 560 First Avenue           \*\    Pager <1> (212) 397 9330                  |
| New York, New York 10016    \**\        <2> 10896   <3> <your-number-here>  |
| (212) 263 5210               \***\_________________________________________ |
| Main machine: karron.med.nyu.edu (128.122.135.3) IRIS 85GT                  |
+-----------------------------------------------------------------------------+

NOTE PHONE NUMBER CHANGE: The Med Ctr has changed from 340 to 263 exchange.