Welcome, Guest. Please login or register.

Author Topic: Looking for ASCII-string -> IEEE double-precision  (Read 1075 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline CosmosTopic starter

  • Hero Member
  • *****
  • Join Date: Jan 2007
  • Posts: 949
    • Show only replies by Cosmos
    • http://leblogdecosmos.blogspot.com
Looking for ASCII-string -> IEEE double-precision
« 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 !

Offline buzz

  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 612
    • Show only replies by buzz
Re: Looking for ASCII-string -> IEEE double-precision
« Reply #1 on: January 01, 2007, 04:22:42 PM »
Doesn't C have atof() ?
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Looking for ASCII-string -> IEEE double-precision
« Reply #2 on: January 01, 2007, 05:09:47 PM »
Yes, it does.

in stdlib.h:

double atof( const char *str );
int p; // A
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Looking for ASCII-string -> IEEE double-precision
« Reply #3 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.