home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mail / qwkrep.zip / CVT.C < prev    next >
Text File  |  1992-09-02  |  2KB  |  59 lines

  1.          /***  MSBIN conversion routines ***/
  2.  
  3.          union Converter
  4.                {
  5.                 unsigned char uc[10];
  6.                 unsigned int  ui[5];
  7.                 unsigned long ul[2];
  8.                 float          f[2];
  9.                 double         d[1];
  10.                }
  11.  
  12.          /* MSBINToIEEE - Converts an MSBIN floating point number */
  13.          /*               to IEEE floating point format           */
  14.          /*                                                       */
  15.          /*  Input: f - floating point number in MSBIN format     */
  16.          /* Output: Same number in IEEE format                    */
  17.  
  18.          float MSBINToIEEE(float f)
  19.          {
  20.             union Converter t;
  21.             int sign, exp;       /* sign and exponent */
  22.  
  23.             t.f[0] = f;
  24.  
  25.          /* extract the sign & move exponent bias from 0x81 to 0x7f */
  26.  
  27.             sign = t.uc[2] / 0x80;
  28.             exp  = (t.uc[3] - 0x81 + 0x7f) & 0xff;
  29.  
  30.          /* reassemble them in IEEE 4 byte real number format */
  31.  
  32.             t.ui[1] = (t.ui[1] & 0x7f) | (exp << 7) | (sign << 15);
  33.             return t.f[0];
  34.          } /* End of MSBINToIEEE */
  35.  
  36.          /* IEEEToMSBIN - Converts an IEEE floating point number  */
  37.          /*               to MSBIN floating point format          */
  38.          /*                                                       */
  39.          /*  Input: f - floating point number in IEEE format      */
  40.          /* Output: Same number in MSBIN format                   */
  41.  
  42.          float IEEEToMSBIN(float f)
  43.          {
  44.             union Converter t;
  45.             int sign, exp;       /* sign and exponent */
  46.  
  47.             t.f[0] = f;
  48.  
  49.          /* extract sign & change exponent bias from 0x7f to 0x81 */
  50.  
  51.             sign = t.uc[3] / 0x80;
  52.             exp  = ((t.ui[1] >> 7) - 0x7f + 0x81) & 0xff;
  53.  
  54.          /* reassemble them in MSBIN format */
  55.  
  56.             t.ui[1] = (t.ui[1] & 0x7f) | (sign << 7) | (exp << 8);
  57.             return t.f[0];
  58.          } /* End of IEEEToMSBIN */
  59.