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!