在.NET中将单个转换为两个UInt16值

Gio*_*Gio 1 .net c c#

在C的旧时代,我可以将浮点数转换为int(假设是32位系统),进行一些位操作(按位和右移等),并获得上下16位十六进制表示浮点数,然后我可以存储在两个短值.我没有在C#中看到一种简单的方法.

System.Convert.ToUInt16只是执行浮点转换为int转换(即使我向右移动),如果浮点数小于0,则保留值0,这不是所需的效果.

//Useless leaves me with a value of 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
Run Code Online (Sandbox Code Playgroud)

基本演员表(UInt32)也不起作用.

Dan*_*haw 7

我想你是在BitConverter课之后.

http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

        float value = 1.1f;
        byte[] bytes = BitConverter.GetBytes(value);
        short upper = BitConverter.ToInt16(bytes, 0);
        short lower = BitConverter.ToInt16(bytes, 2);
Run Code Online (Sandbox Code Playgroud)