views:

4459

answers:

8

Hi,

What I'm trying to do is to convert a double to hex string and then back to double.

The following code does conversion double-to-hex string.

char * double2HexString(double a)
{
   char *buf = new char[17]; // double is 8-byte long, so we have 2*8 + terminating \0
   char *d2c;
   d2c = (char *) &a;
   char *n = buf;
   int i;
   for(i = 0; i < 8; i++)
   {
      sprintf(n, "%02X", *d2c++);
      n += 2;
   } 
   *(n) = '\0';
}

This seems work, however, I'm not sure how to convert the resulting string back to double. Please advise :)

A: 

Using sprintf is slow, to be honest, but you can revert it with sscanf, doing almost exactly the same thing.

Well, actually, you'd have to copy each two characters to a buffer string, to decode each individually. My first try, below is incorrect:

double hexString2Double(char *buf)
{
  char *buf2 = new char[3];
  double a;
  char* c2d;
  c2d = (char *) &a;
  int i;

  buf2[2] = '\0'

  for(i = 0; i < 16; i++)
  {
    buf2[0] = *buf++;
    buf2[1] = *buf++;
    sscanf(buf2, "%X", c2d++);
  }

  return a;
}

You see, %X is decoded as an int, not as a byte. It might even work, depending on low-ending/high-endian issues, but it's basically broken. So, let's try to get around that:

double hexString2Double(char *buf)
{
  char *buf2 = new char[3];
  double a;
  char* c2d;
  c2d = (char *) &a;
  int i;
  int decoder;

  buf2[2] = '\0'

  for(i = 0; i < 16; i++)
  {
    buf2[0] = *buf++;
    buf2[1] = *buf++;
    sscanf(buf2, "%X", &decoder);
    c2d++ = (char) decoder;
  }

  return a;
}

Barring syntax errors and such, I think this should work.

Daniel
+1  A: 
char *doubleToRawString(double x) {
    const size_t bytesInDouble = 8;

    union {
        double value;
        unsigned char bytes[bytesInDouble];
    } u;

    u.value = x;

    char *buffer = new char[bytesInDouble * 2 + 1];
    unsigned char *input = u.bytes;
    char *output = buffer;

    for(int i = 0; i < bytesInDouble; ++i) {
        sprintf(output, "%02hhX", *input);

        ++input;
        output += 2;
    }

    return buffer;
}

double rawStringToDouble(const char *input) {
    const size_t bytesInDouble = 8;

    union {
        double value;
        unsigned char bytes[bytesInDouble];
    } u;

    unsigned char *output = u.bytes;

    for(int i = 0; i < bytesInDouble; ++i) {
        sscanf(input, "%02hhX", output);

        input += 2;
        ++output;
    }

    return u.value;
}

This uses the non-standard hh modifier. If you don't want to use that, use:

unsigned int tmp = *input;
sprintf(output, "%02X", tmp);

unsigned int tmp;
sscanf(input, "%02X", &tmp);
*output = tmp;
strager
A: 

Almost the same procedure should do

void hex2double(const char* buf, double& a)
{
   char tmpbuf[3]={0};
   char *d2c;
   unsigned int tmp;
   d2c = (char *) &a;
   char *n = buf;
   int i;
   for(i = 0; i < 8; i++)
   {
      tmpbuf[0]=*buf++;
      tmpbuf[1]=*buf++;
      sscanf(tmpbuf, "%X", &tmp);
      *d2c++=tmp;
   }
}

Quick & dirty.

Note, however, that this is playing with fire. First, your hex strings are only usable on machines with the same double format, and the same endianness. Second, the conversion functions are short on strict aliasing rule.

jpalecek
+1  A: 
char *doubleToRawString(double x) {
    // Assumes sizeof(long long) == 8.

    char *buffer = new char[32];
    sprintf(buffer, "%llx", *(unsigned long long *)&x);  // Evil!
    return buffer;
}

double rawStringToDouble(const char *s) {
    // Assumes sizeof(long long) == 8.

    double ret;
    sscanf(s, "%llx", (unsigned long long *)&ret);  // Evil!
    return ret;
}
strager
A: 
#include <stdio.h>
main() {
  union double_ull_t {
    double d;
    unsigned long long u;
  } x;
  scanf("%lf",&x.d);
  printf("%016llX %lf\n",x.u,x.d);
  scanf("%016llX",&x.u);
  printf("%016llX %lf\n",x.u,x.d);
}

Maybe not the most efficient solution, but the easiest to code.

Sparr
looks like strager beat me to this particular approach, and with more applicable implementation
Sparr
A: 
dwelch
A: 
Sai Krishna V
A: 

i need some simple program using stack

nayana