[R] Floats in Microsoft Basic format

Duncan Murdoch murdoch.duncan at gmail.com
Wed Aug 10 15:34:47 CEST 2011


On 10/08/2011 5:58 AM, taraxacum wrote:
> 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)

You'll need to write your own function.  It can be very similar to what 
you have below, except:
  - you'll need to produce double rather than single (easy, just produce 
a single and assign to a double, C does the conversion for you)
  - you need to change the args to your function to some that R understands.

Duncan Murdoch

> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list