[R] Floats in Microsoft Basic format

taraxacum bvalen at alice.it
Wed Aug 10 11:58:29 CEST 2011


Hi all,
I need to convert a floating point value from Microsoft Basic format to IEEE
format.
There's a simple way to achieve this in R or I have to write my own
function?
(e.g. convert the C code below)

thanks
t

#include <string.h>        /* for strncpy  */

  int _fmsbintoieee(float *src4, float *dest4)
     {
     unsigned char *msbin = (unsigned char *)src4;
     unsigned char *ieee = (unsigned char *)dest4;
     unsigned char sign = 0x00;
     unsigned char ieee_exp = 0x00;
     int i;

     /* MS Binary Format                         */
     /* byte order =>    m3 | m2 | m1 | exponent */
     /* m1 is most significant byte => sbbb|bbbb */
     /* m3 is the least significant byte         */
     /*      m = mantissa byte                   */
     /*      s = sign bit                        */
     /*      b = bit                             */

     sign = msbin[2] & 0x80;      /* 1000|0000b  */

     /* IEEE Single Precision Float Format       */
     /*    m3        m2        m1     exponent   */
     /* mmmm|mmmm mmmm|mmmm emmm|mmmm seee|eeee  */
     /*          s = sign bit                    */
     /*          e = exponent bit                */
     /*          m = mantissa bit                */

     for (i=0; i<4; i++) ieee[i] = 0;

     /* any msbin w/ exponent of zero = zero */
     if (msbin[3] == 0) return 0;

     ieee[3] |= sign;

     /* MBF is bias 128 and IEEE is bias 127. ALSO, MBF places   */
     /* the decimal point before the assumed bit, while          */
     /* IEEE places the decimal point after the assumed bit.     */

     ieee_exp = msbin[3] - 2;    /* actually, msbin[3]-1-128+127 */

     /* the first 7 bits of the exponent in ieee[3] */
     ieee[3] |= ieee_exp >> 1;

     /* the one remaining bit in first bin of ieee[2] */
     ieee[2] |= ieee_exp << 7;

     /* 0111|1111b : mask out the msbin sign bit */
     ieee[2] |= msbin[2] & 0x7f;

     ieee[1] = msbin[1];
     ieee[0] = msbin[0];

     return 0;
     }


--
View this message in context: http://r.789695.n4.nabble.com/Floats-in-Microsoft-Basic-format-tp3732456p3732456.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list