AnswerBun.com

Better way to convert decimal float into special hexadecimal notation

I have decimal numbers in the range between 0 and ~15.9 and want to convert them into a specific hexadecimal notation.
So the special notation goes like this:
There are 16 bits. The first 4 bits are for the pre-decimal-point position. The next 12 bits are for the decimal places.

A * 16^0 + B * 16^{-1} + C * 16^{-2} + D * 16^{-3}
Image of previous equation

Here is how I am doing it now in C++ (I am "brute forcing" it. Just check how often 16 fits in A, then check how often 1/16 fits in B and so on):

uint16_t dec_to_special_hex(double x){
    uint16_t A, B, C, D;
    A = static_cast<uint16_t>(x);
    if (A > 15)
        return 65535;

    double remainder = x - A;    //get decimal places
    B = static_cast<uint16_t>(remainder * 16);
    remainder = remainder - B * (1 / 16);
    C = static_cast<uint16_t>(remainder * 16 * 16);
    remainder = remainder - C * (1 / (16 * 16));
    D = static_cast<uint16_t>(remainder * 16 * 16 * 16);
    remainder = remainder - D * (1 / (16 * 16 * 16));


    uint16_t temp, ret = A;
    ret = ret << 12;
    temp = B << 8;
    ret = ret | B;
    temp = C << 4;
    ret = ret | C;
    ret = ret | D;
    return ret;
}

I wounder if there is no better, more elegant way to do this. I am looking at the digits I am handling and it just feels like there must be more to it, but I could not find a better way yet.
Would be happy for your suggestions!

Stack Overflow Asked by MYZ on January 1, 2021

2 Answers

2 Answers

I wounder if there is no better, more elegant way to do this

Elegant is subjective, but:

One thing you can do is work with the bits directly. Check this simple converter to see how IEEE floating point numbers work (if you don't know already). Then look at this question for a method to get the different groups of bits of a floating point number (the answer therein is for a float type, but the conversion to double is simple).

After you have the bits, All you need to do is shift the mantissa, depending on the exponent, and perform bit-wise & with different windows to get the bits for A, B, C and D.

Correct answer by Benny K on January 1, 2021

Assuming you just wish to truncate and not round, this can be done with:

uint16_t dec_to_special_hex(double x)
{
    return
        x < 0   ?     0 :
        16 <= x ? 65535 :
                  4096*x;
}

Answered by Eric Postpischil on January 1, 2021

Add your own answers!

Related Questions

Creating a Message for Gmail API in C#

3  Asked on December 1, 2021 by muttley91

   

how do I seed data to firebase firestore

4  Asked on December 1, 2021 by sambulo-senda

     

OpenCV Frame Grabber not work on MacOS Catalina

1  Asked on December 1, 2021 by wojciech_cichocki

         

relative rotation in 3D

2  Asked on December 1, 2021

   

How to Mock the ActivityContext Class

0  Asked on December 1, 2021 by mathiyazhagan

       

Nested array into array, adding brackets around nested array

1  Asked on December 1, 2021 by stackaccount1

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.