Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

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