Answered convert floating point to fixed and back

Maylar

Member
Joined
Jan 22, 2021
Messages
16
Programming Experience
10+
I have an application that will stuff fixed point values into CPU registers. They're 32 bit (4 byte) and specified as:

Unsigned fixed-point number: 5 bits integer 27 bits fractional
Min = 0x0 = 0.0000000 Max = 0xFFFFFFFF = 31.9999995

How would I convert a number like 1.003936 to 4 bytes? Would also know how to go the other way Using C# or C++

TIA
 
How do you do it manually? You would do the same things exactly the same way with code.
 
This is a C# forum so why even suggest C++?

Anyway, you might want to look into the BitConverter class. That said, the BitConverter.GetBytes(double) method returns an array of 8 bytes, so you may need a little jiggery-pokery to get exactly what you want.
 
So this isn't even a programming question. This is a basic data encoding question.

If you are doing this for a class, presumably your teacher has covered how fixed point values are stored in memory/CPU registers.

If you are doing this for work or a hobby, presumably the hardware manual that is requiring you to put in the values as fixed point values into CPU registers shows how values are stored.
 
Try the suggestion on post 3.

Then come back with the code you wrote based off of the documentation you looked up, as suggested above.

When you have a problem with code you've actually written yourself, then we feel more obliged to give assistance. On this forum, it is also a place of learning, and on that note, we need to first see what effort you've attempted on your part.
 
This is a C# forum so why even suggest C++?

Anyway, you might want to look into the BitConverter class. That said, the BitConverter.GetBytes(double) method returns an array of 8 bytes, so you may need a little jiggery-pokery to get exactly what you want.
Unfortunately, it's the jiggery-pokery that eludes me. Bitconverter doesn't have a method of specifying the mantissa and exponent lengths. Thanks anyway.
 
So this isn't even a programming question. This is a basic data encoding question.

If you are doing this for a class, presumably your teacher has covered how fixed point values are stored in memory/CPU registers.

If you are doing this for work or a hobby, presumably the hardware manual that is requiring you to put in the values as fixed point values into CPU registers shows how values are stored.
Yes, it's more of a math question, though I will be implementing the solution in C#.
The application is for work, and the documentation for the processor doesn't give any clue what algorithm they're using other than the register description that I posted above.

I'll see if I can find a math forum.
 
Unfortunately, that description in post one gives us no clue whether the 5 bits of the integer part are in the low part of the register or the high part of the register. Are you sure that there are no other sample values? The convention is that it's the high part to simplify comparisons, but there's no guarantee that is the layout they are expecting.
 
Unfortunately, it's the jiggery-pokery that eludes me. Bitconverter doesn't have a method of specifying the mantissa and exponent lengths. Thanks anyway.
For fixed point you don't need to deal with mantissa and exponent. Everything is base 2 for fixed point (unless your hardware manual is saying otherwise).
 
Thanks I'll see if I can get clarification from the CPU manufacturer. Unfortunately, I deal with their field app guys here in the states, who forward my questions to the mfgr in Germany, so there's a bit of a lag in time.
Math isn't my strong point :(
 
Back
Top Bottom