FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour IEEE to MSBIN converter
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
IEEE to MSBIN converter
Posted: Sat Jan 17, 2015 07:22 PM
Dear friends, I need to convert from IEEE to MSBIN numeric format. Currently I'm using the function below (found on Internet) but I just discovered that it fails with -0.328 and other similar values. Any suggestions?

Code (fw): Select all Collapse
int _fieeetomsbin(float *src4, float *dest4)
{
    unsigned char *ieee = (unsigned char *)src4;
    unsigned char *msbin = (unsigned char *)dest4;
    unsigned char sign;
    unsigned char msbin_exp = 0x00;
    int i;

    /* See _fmsbintoieee() for details of formats   */
    sign = ( unsigned char ) ( ieee[3] & 0x80 );
    msbin_exp |= ( unsigned char ) ( ieee[3] << 1 );
    msbin_exp |= ( unsigned char ) ( ieee[2] >> 7 );

    /* An ieee exponent of 0xfe overflows in MBF    */
    if (msbin_exp == 0xfe) return 1;

    msbin_exp += ( unsigned char ) 2;     /* actually, -127 + 128 + 1 */

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

    msbin[3] = msbin_exp;

    msbin[2] |= sign;
    msbin[2] |= ( unsigned char ) ( ieee[2] & 0x7f );
    msbin[1] = ieee[1];
    msbin[0] = ieee[0];

    return 0;
}


EMG
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: IEEE to MSBIN converter
Posted: Sun Jan 18, 2015 06:29 PM

Enrico,

The inner format of float numbers is different from one C compiler to another.

What C compiler was that example for ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: IEEE to MSBIN converter
Posted: Sun Jan 18, 2015 07:45 PM

Antonio,

it should be for Borland, what I'm using.

EMG

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: IEEE to MSBIN converter
Posted: Sun Jan 18, 2015 08:26 PM
Enrico,

See this:

Code (fw): Select all Collapse
int _fieeetomsbin(float *src4, float *dest4)
     {
     unsigned char *ieee = (unsigned char *)src4;
     unsigned char *msbin = (unsigned char *)dest4;
     unsigned char sign = 0x00;
     unsigned char msbin_exp = 0x00;
     int i;

     /* See _fmsbintoieee() for details of formats   */
     sign = ieee[3] & 0x80;
     msbin_exp |= ieee[3] << 1;
     msbin_exp |= ieee[2] >> 7;

     /* An ieee exponent of 0xfe overflows in MBF    */
     if (msbin_exp == 0xfe) return 1;

     msbin_exp += 2;     /* actually, -127 + 128 + 1 */

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

     msbin[3] = msbin_exp;

     msbin[2] |= sign;
     msbin[2] |= ieee[2] & 0x7f;
     msbin[1] = ieee[1];
     msbin[0] = ieee[0];

     return 0;
     }


http://files.mpoli.fi/unpacked/software/programm/c/c_all.zip/ti1400.asc

It seems as it is for Borland:
// The following are implementations of Microsoft RTL functions
// not include in the Borland RTL.
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: IEEE to MSBIN converter
Posted: Sun Jan 18, 2015 08:28 PM

ops, thats the one you are using :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: IEEE to MSBIN converter
Posted: Sun Jan 18, 2015 10:57 PM

Enrico,

They are just 4 bytes, so maybe you could do the conversion from PRG code and inspect each byte.

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: IEEE to MSBIN converter
Posted: Mon Jan 19, 2015 09:17 AM

Antonio,

I already done all the tests I can imagine. The problem is that I don't know what the result should be. What I know for sure is that it's failing (GPF) for some values. :-(

EMG

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: IEEE to MSBIN converter
Posted: Mon Jan 19, 2015 09:53 AM

Enrico,

So the problem is a GPF ?

Don't you get a hb_out.log with xHarbour to report the GPF origin ?

Is the GPF coming from that function ?

We could place some traces into it...

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: IEEE to MSBIN converter
Posted: Mon Jan 19, 2015 10:25 AM

Antonio,

yes, I already traced the error in how the function _fieeetomsbin() fills the float number producing a NAN for some values.

EMG

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: IEEE to MSBIN converter
Posted: Mon Jan 19, 2015 10:48 PM
This is a complete sample using a different function that shows the problem too:

Code (fw): Select all Collapse
#include <stdio.h>


int _fieeetomsbin( float *src, float *dst )
{
    union
    {
        float a;
        unsigned long b;
    } c;

    unsigned short man;
    unsigned short exp;

    c.a = *src;

    if ( c.b )
    {
        man = c.b >> 16;
        exp = ( ( man << 1 ) & 0xff00 ) + 0x0200;

        if ( ( exp & 0x8000 ) != ( ( man << 1 ) & 0x8000 ) )
            return 1;

        man = ( man & 0x7f ) | ( ( man >> 8 ) & 0x80 );
        man |= exp;
        c.b = ( c.b & 0xffff ) | ( ( long ) man << 16 );
    }

    *dst = c.a;

    return 0;
}


float IeeToMsb( double nTmp )
{
    float nIee = ( float ) nTmp;
    float nMsb;

    _fieeetomsbin( &nIee, &nMsb );

    return nMsb;
}


int main()
{
    printf ( "%f\n", IeeToMsb( 0.328 ) );
    printf ( "%f\n", IeeToMsb( -0.328 ) );

    return 0;
}


EMG
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: IEEE to MSBIN converter
Posted: Tue Jan 20, 2015 07:01 AM

Enrico,

Does it GPF ?

If so, where ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: IEEE to MSBIN converter
Posted: Tue Jan 20, 2015 08:33 AM

Antonio,

with Borland it GPFs on return of the function _fieeetomsbin(). I tried three different online C compilers and got NAN (Not A Number) in the second printf().

EMG

Continue the discussion