IEEE-754 code to convert to and fro double/hex string 
Author Message
 IEEE-754 code to convert to and fro double/hex string

Hi all, i would like to find a way on windows api or some code c/c++ code to
convert a hex string representing the bits of a double to a double and
vice-versa.  atof does not seem to be precise enough, since it truncates the
double at 8 char (digits) after the floating point.  I need to go to 15.  I
can easily take a double and convert it to long bits on my java side, my c++
server needs to take this long (sent as a string, either hex ot not) and
remake the bits for a double from it.  Does anyone know where i can find
such a thing?

Tx.
--



Mon, 18 Aug 2003 06:00:24 GMT  
 IEEE-754 code to convert to and fro double/hex string

Quote:

> Hi all, i would like to find a way on windows api or some code c/c++ code to
> convert a hex string representing the bits of a double to a double and
> vice-versa.  atof does not seem to be precise enough, since it truncates the
> double at 8 char (digits) after the floating point.  I need to go to 15.  I
> can easily take a double and convert it to long bits on my java side, my c++
> server needs to take this long (sent as a string, either hex ot not) and
> remake the bits for a double from it.  Does anyone know where i can find
> such a thing?

The 'windows api' might be enough to turn some people away from your question,
as might the reference to the non-existent language 'c/c++'.

However, since you are posting to comp.lang.c, I will assume that C is the
intended language and the platform is non-relevant (otherwise the question
belongs in an implementation-specific newsgroup).

It is really unclear what you mean by "bits of a double".

Here's a bit of code to play with:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    double realnumber = 0.123456789012345;
    unsigned char characters[sizeof(double)];
    char teststring[3 + 2 * sizeof(double)];
    unsigned int integers[sizeof(double) / sizeof(int)];
    size_t i;
    unsigned long long testint;

    printf("The double we are using is %.15f\n", realnumber);
    memmove(characters, &realnumber, sizeof(double));
    memmove(integers, &realnumber, sizeof(double));
    printf("We have copied the double into arrays of "
           "characters and ints.\n"
           "Let's see what they look like in hex:\n\n");
    printf("Array of %u chars\n0x: ", (unsigned)sizeof characters);
    for (i = 0; i < sizeof characters; i++)
        printf("%0x ", characters[i]);
    printf("\n");
    printf("Array of %u ints\n0x: ",
           (unsigned)(sizeof integers / sizeof *integers));
    for (i = 0; i < sizeof integers / sizeof *integers; i++)
        printf("%0x ", integers[i]);
    printf("\n\n");

    printf("Let's try something else.\n");
    if (sizeof testint < sizeof realnumber) {
        printf("OOPS!\n");
        exit(EXIT_FAILURE);
    }
    memmove(&testint, &realnumber, sizeof realnumber);
    printf("We have stored the double in a long long.\n"
           "The long long contains: %#Lx\n", testint);
    printf("This suggests we can produce a string for it.\n");
    if (sizeof realnumber == sizeof(long long))
        sprintf(teststring, "%#Lx", testint);
    else if (sizeof realnumber == sizeof(long))
        sprintf(teststring, "%#lx", *(unsigned long *)&testint);
    else if (sizeof realnumber == sizeof(short))
        sprintf(teststring, "%#x", *(unsigned short *)&testint);
    else if (sizeof realnumber == sizeof(char))
        sprintf(teststring, "%#x", *(unsigned char *)&testint);
    else {
        printf("OOPS!\n");
        exit(EXIT_FAILURE);
    }
    printf("The string we created is \"%s\"\n", teststring);
    printf("Now we will zero out the long long and the double\n");
    testint = 0;
    realnumber = 0;
    printf("unsigned long long testint = %#Lx, double realnumber = %f\n",
           testint, realnumber);
    printf("We will read the testint from the string: ");
    sscanf(teststring, "%Lx", &testint);
    printf("%#Lx\n", testint);
    printf("Move it into the double ... ");
    memmove(&realnumber, &testint, sizeof realnumber);
    printf("Which is now %.15f\n", realnumber);
    return 0;

Quote:
}

The double we are using is 0.123456789012345
We have copied the double into arrays of characters and ints.
Let's see what they look like in hex:

Array of 8 chars
0x: 2e f6 46 37 dd 9a bf 3f
Array of 2 ints
0x: 3746f62e 3fbf9add

Let's try something else.
We have stored the double in a long long.
The long long contains: 0x3fbf9add3746f62e
This suggests we can produce a string for it.
The string we created is "0x3fbf9add3746f62e"
Now we will zero out the long long and the double
unsigned long long testint = 0, double realnumber = 0.000000
We will read the testint from the string: 0x3fbf9add3746f62e
Move it into the double ... Which is now 0.123456789012345



Mon, 18 Aug 2003 08:30:34 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. convert IEEE 754 single-precision 32-bit float to its corresponding bits

2. Convert IBM Floating Point to IEEE 754

3. IEEE-754 floats in C

4. Detection of "IEEE 754"

5. dealing with IEEE STD 754 real number in C

6. IEEE 754 or 854 FP representation standard

7. function for IEEE 754 rounding

8. Converting a string containing hex to a double

9. Comparing double NAN with double INF is not IEEE compliant

10. Double 2 Hex-String

11. Convert Hex String to actual Characters?

12. convert hex to ascii in a string

 

 
Powered by phpBB® Forum Software