Convert int bits to float bits

前端 未结 6 1273
醉话见心
醉话见心 2020-12-04 02:50

I\'m in the process of creating a buffer that will read/write in a banner in which I can completely eradicate the problems that comes with TCP-Segmentation. The only problem

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 03:27

    BitConverter creates some overhead and unnecessary buffer. This solution is almost as fast as unsafe conversion:

    [StructLayout(LayoutKind.Explicit)]
    struct FloatToInt 
    {
        [FieldOffset(0)]private float f;
        [FieldOffset(0)]private int i;
        private static FloatToInt inst = new FloatToInt();
        public static int Convert(float value)
        {
            inst.f = value;
            return t.i;
        }
    }
    

提交回复
热议问题