From: Koray on
Hi,
I need to convert a "uint16" into a fixed point format value as 16 bits word with 8 bits fraction.
For example:
I have uint16 number, say binary 0000.0001.1000.0000
(So fixed point correspondance should be 1.5)
How can I get fixed point value of this number(1.5)?
From: Walter Roberson on
Koray wrote:
> Hi,
> I need to convert a "uint16" into a fixed point format value as 16 bits
> word with 8 bits fraction. For example:
> I have uint16 number, say binary 0000.0001.1000.0000
> (So fixed point correspondance should be 1.5)
> How can I get fixed point value of this number(1.5)?

(N / 256) + double(mod(N,256)) / 256

The (N/256) part must be done with N still uint16. Alternately,

floor(N/256) + double(mod(N,256)) / 256

will work even if the decimal equivalent of the binary has been entered
(i.e., N = 384 in this case) without requiring N to be uint16.
From: Koray on
Thank you for your help Walter. Your solution really worked for unsigned representation in uint16. Actually I represent a two's complement signed number in uint16. After some search I found a way to convert uint16 to int16 using,

N = typecast(uint16(N),'int16');

I just wanted to share. Thank you again for your quick response.