0

I have uint64_t hex variable of the double variable, I need to convert it to double.

I am using below function for the converting operation using 8 bits hex array:

double HexToDoubleConverter(uint8_t *hexArray)
{
    double convertedValue = 0.0;
    memcpy(&convertedValue, hexArray, sizeof(convertedValue));
    return convertedValue;
}

And I change with like this:

double U64ToDoubleConverter(uint64_t val)
{
    double convertedValue = 0.0;
    memcpy(&convertedValue, val, sizeof(convertedValue));
    return convertedValue;
}

But it didnt work for me how can I convert correctly?

wohlstad
  • 12,661
  • 10
  • 26
  • 39
hobik
  • 439
  • 4
  • 15
  • 1
    Pointer and value are different things. – KagurazakaKotori Jun 30 '21 at 07:17
  • How can I convert uint64_t to Double ? – hobik Jun 30 '21 at 07:17
  • Is `convertedValue = val;` not enough for your use case? – Serge Ballesta Jun 30 '21 at 07:18
  • You can't do that without possibly losing significance. Is this the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – Weather Vane Jun 30 '21 at 07:18
  • 3
    `&val` would be more likely to succeed, you're right now using the _value_ of val as a pointer, instead of the _pointer_ to val. – Joachim Isaksson Jun 30 '21 at 07:21
  • Converting is simple: `doubleVariable = uint64Variable;` Done.... But I have a feeling that you are not looking for a conversion.... Seems to be about low-level bit-pattern instead !? – Support Ukraine Jun 30 '21 at 07:23
  • I dont understant how could be equal doubleVariable = uint64Variable ? – hobik Jun 30 '21 at 07:27
  • 1
    `double U64ToDoubleConverter(uint64_t val ) { return val; }`? `how can I covert correctly ?` Please define "correctly"? what does it mean? For what input (what value of uint64_t variable) what output (what value of double) is "correct"? – KamilCuk Jun 30 '21 at 07:33
  • @hobik please [edit] and show some code that calls your functions and show the exepected vs. actual output. Read this: [ask] and this: [mcve] – Jabberwocky Jun 30 '21 at 07:53
  • @hobik I'm not sure at all, but maybe you want this: `memcpy(&convertedValue,val, sizeof(convertedValue));` -> `memcpy(&convertedValue, &val, sizeof(convertedValue));` – Jabberwocky Jun 30 '21 at 07:55

1 Answers1

2

You probably want this:

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

double U64ToDoubleConverter(uint64_t val);
uint64_t DoubleToU64Converter(double val);

int main()
{
  double f = 15.63334;
  uint64_t u = DoubleToU64Converter(f);
  double fconvedtedback = U64ToDoubleConverter(u);

  printf("Original double: %lf\n", f);
  printf("double converted to uint64_t: %llx\n", u);
  printf("uint64_t converted back to double: %lf\n", fconvedtedback);

  if (f == fconvedtedback)
    printf("Test succeeded\n");
  else
    printf("Test did not succeede\n");

  return 0;
}

uint64_t DoubleToU64Converter(double val)
{
  uint64_t convertedValue = 0;
  memcpy(&convertedValue, &val, sizeof(convertedValue));
  return convertedValue;
}

double U64ToDoubleConverter(uint64_t val)
{
  double convertedValue = 0.0;
  memcpy(&convertedValue, &val, sizeof(convertedValue));
  return convertedValue;
}

Here we convert a double 15.63334 to an uint64_t and then we convert the uint64_t back to a double and we display all three values.

Possible output:

Original double: 15.633340
double converted to uint64_t: 402f444523f67f4e
uint64_t converted back to double: 15.633340
Test succeeded
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115