# 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}

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

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

## Related Questions

### Scanner is skipping nextLine() after using next() or nextFoo()?

23  Asked on December 1, 2021 by blekione

### Creating a Message for Gmail API in C#

3  Asked on December 1, 2021 by muttley91

### exclude certain projects from using Directory.Build.props

2  Asked on December 1, 2021 by user1367984

### Angular custom webpack config enable CSS modules

1  Asked on December 1, 2021 by grinay

### How can i save the fulfillmentText in dialogflow to firebase?

1  Asked on December 1, 2021 by reniel-mallari

### how do I seed data to firebase firestore

4  Asked on December 1, 2021 by sambulo-senda

### How to speed up dataframe std() calculation on each row?

2  Asked on December 1, 2021

### “Operation canceled” Realm Error Domain=io.realm.unknown Code=89

0  Asked on December 1, 2021 by coconuts

### 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

### Scaffold-DbContext doese not add [DatabaseGenerated(DatabaseGeneratedOption.Computed)] on Generated class

1  Asked on December 1, 2021

### PHP+SOAP. Is it possible to make multiple tries creating new SoapClient?

1  Asked on December 1, 2021 by alexander-rusanov

1  Asked on December 1, 2021

### How to Mock the ActivityContext Class

0  Asked on December 1, 2021 by mathiyazhagan

### How to execute a function from second ViewController after popViewController?

1  Asked on December 1, 2021

### Nested array into array, adding brackets around nested array

1  Asked on December 1, 2021 by stackaccount1

### When I compile my code I get a message what does it mean

2  Asked on December 1, 2021 by mr-hones

### Is it possible to override the create_migration template in Rails 6?

2  Asked on December 1, 2021 by ivn-quiones

### “Unresolved import” error for Python built-ins in vscode on macOS

0  Asked on December 1, 2021 by safetymonkey

### Plugin: Waterwheel Image Carousel. How do I add only text?

1  Asked on December 1, 2021