[英]Converting uint64_t to Double Value
我有雙變量的 uint64_t 十六進制變量,我需要將其轉換為雙精度。
我正在使用以下函數進行使用 8 位十六進制數組的轉換操作:
double HexToDoubleConverter(uint8_t *hexArray)
{
double convertedValue = 0.0;
memcpy(&convertedValue, hexArray, sizeof(convertedValue));
return convertedValue;
}
我這樣改變:
double U64ToDoubleConverter(uint64_t val)
{
double convertedValue = 0.0;
memcpy(&convertedValue, val, sizeof(convertedValue));
return convertedValue;
}
但它對我不起作用,我怎樣才能正確轉換?
你可能想要這個:
#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;
}
在這里,我們將雙15.63334
轉換為uint64_t
,然后將uint64_t
轉換回double
精度並顯示所有三個值。
可能的輸出:
Original double: 15.633340
double converted to uint64_t: 402f444523f67f4e
uint64_t converted back to double: 15.633340
Test succeeded
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.