Conversion of Single to two UInt16 values in .net

Posted by Gio on Stack Overflow See other posts from Stack Overflow or by Gio
Published on 2010-04-30T18:12:51Z Indexed on 2010/04/30 18:17 UTC
Read the original article Hit count: 122

Filed under:
|
|
|

In the good old days of C. I could cast a float to an int (assuming 32 bit system), do some bit manipluation ( bitwise and, right shift, ect ), and get the upper and lower 16 bit hex representations of the floating point number, which I could then store in two short values. I'm not seeing an easy way of doing this in C#.
System.Convert.ToUInt16 just does a float to int convert (even after I shift right), which leaves a vlaue of 0 if the float is less than 0, which is not the desired effect.

//useless leaves me witg a value 0f 0
UIN16 s1 = (UInt16)((System.Convert.ToUInt32(d2) & 0xffff0000) >> 16);    //capture the high word
UInt16 s2 = (UInt16)(System.Convert.ToUInt32(d2) & 0xffff);              //capture the low word

A basic cast (UInt32) doesn't work either.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#