How to convert 64 bit hex value to double in c?

Omkar Dixit

I'm using a gps module through which I'm getting the string

"0x3f947ae147ae147b"

which I need to convert to double. The expected value is 0.02.

I referred the following website for the reference

https://gregstoll.com/~gregstoll/floattohex/

How I can convert value in the C?

Eric Postpischil

3F947AE147AE147B16 is the encoding for an IEEE-754 binary64 (a.k.a. “double precision”) datum with value 0.0200000000000000004163336342344337026588618755340576171875. Supposing your C implementation uses that format for double and has 64-bit integers with the same endianness, you can decode it (not convert it) by copying its bytes into a double and printing them:

#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
    char *string = "0x3f947ae147ae147b";

    //  Set errno to zero before using strtoull.
    errno = 0;
    char *end;
    unsigned long long t = strtoull(string, &end, 16);

    //  Test whether stroull did not accept all characters.
    if (*end)
    {
        fprintf(stderr,
            "Error, string \"%s\", is not a proper hexadecimal numeral.\n",
            string);
        exit(EXIT_FAILURE);
    }

    //  Move the value to a 64-bit unsigned integer.
    uint64_t encoding = t;

    /*  Test whether the number is too large, either because strtoull reported
        an error or because it does not fit in a uint64_t.
    */
    if ((t == ULLONG_MAX && errno) || t != encoding)
    {
        fprintf(stderr, "Error, string \"%s\", is bigger than expected.\n",
            string);
        exit(EXIT_FAILURE);
    }

    //  Copy the bytes into a double.
    double x;
    memcpy(&x, &encoding, sizeof x);
    printf("%.9999g\n", x);
}

This should output “0.0200000000000000004163336342344337026588618755340576171875”.

If your C implementation does not support this format, you can decode it:

  • Separate the 64 bits into s, e, f, where s is the leading bit, e is the next 11 bits, and f is the remaining 52 bits.
  • If e is 2047 and f is zero, report the value is +∞ or −∞, according to whether s is 0 or 1, and stop.
  • If e is 2047 and f is not zero, report the value is a NaN (Not a Number) and stop.
  • If e is not zero, add 252 to f. If e is zero, change it to one.
  • The magnitude of the represented value is f•2−52•2e−1023, and its sign is + or − according to whether s is 0 or 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive