Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Cosmos Amiga on January 01, 2007, 04:16:10 PM

Title: Looking for ASCII-string -> IEEE double-precision
Post by: Cosmos Amiga on January 01, 2007, 04:16:10 PM
Hi !


I'm looking for a routine (C or asm) for
converting ASCII-string into IEEE double-precision.

For example :

0.01 -> $3F84,$7AE1,$47AE,$147B


And into .x precision too !



Thanxs a lot !
Title: Re: Looking for ASCII-string -> IEEE double-precision
Post by: buzz on January 01, 2007, 04:22:42 PM
Doesn't C have atof() ?
Title: Re: Looking for ASCII-string -> IEEE double-precision
Post by: Karlos on January 01, 2007, 05:09:47 PM
Yes, it does.

in stdlib.h:

double atof( const char *str );
Title: Re: Looking for ASCII-string -> IEEE double-precision
Post by: Piru on January 01, 2007, 05:10:51 PM
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  int i;
  for (i = 1; i < argc; i++)
  {
    struct
    {
      union
      {
        double d;
        unsigned short w[4];
      } un;
    } p;
    p.un.d = atof(argv[i]);
    printf(&quot;$%04x,$%04x,$%04x,$%04x\n&quot;,
#ifdef __GNUC__
#if #cpu (m68k) || #cpu (powerpc)
           p.un.w[0], p.un.w[1], p.un.w[2], p.un.w[3]);
#elif #cpu (i386)
           p.un.w[3], p.un.w[2], p.un.w[1], p.un.w[0]);
#else
#warning Unknown CPU, assuming big-endian
           p.un.w[0], p.un.w[1], p.un.w[2], p.un.w[3]);
#endif
#else
/* Unknown compiler, assuming big-endian */
           p.un.w[0], p.un.w[1], p.un.w[2], p.un.w[3]);
#endif
  }
  return 0;
}


Note: This code is not portable. It depends on internal representation of the double and short variables. It should work with m68k, ppc (32-bit) and x86 (32-bit), at least.