32位浮点数和整数互转

261 阅读1分钟

udata32可以是uint8 buf[1],buf[2],buf[3],buf[4]; 

unsafe private float HexToFloat(UInt32 udata32)

        {

            float f32;

            byte* p = (byte*)&(f32);

            *(p) = (byte)udata32;

            *(p + 1) = (byte)(udata32 >> 8);

            *(p + 2) = (byte)(udata32 >> 16);

            *(p + 3) = (byte)(udata32 >> 24);

            return f32;

        }

 

        unsafe private UInt32 FloatToHex(float f32)

        {

            UInt32 udata32;

            byte* p = (byte*)&(f32);

            udata32 = ((UInt32)(*(p + 3)) << 24) + ((UInt32)(*(p + 2)) << 16) + ((UInt32)(*(p + 1)) << 8) + (UInt32)(*p);

            return udata32;

        }