Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started 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 !
-
Doesn't C have atof() ?
-
Yes, it does.
in stdlib.h:
double atof( const char *str );
-
#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("$%04x,$%04x,$%04x,$%04x\n",
#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.