#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.