串口通讯浮点型与整形(4字节)转换

float floatValue =100f;

            byte[] bytes = BitConverter.GetBytes(floatValue);

            int intValue = 0;

            intValue = BitConverter.ToInt32(bytes, 0);

            string str = string.Format("{0:X8}", intValue);

            MessageBox.Show(str);

 

//逆转换(4字节数字高位在前,字节数组低位在前):

            byte[] bt1 = BitConverter.GetBytes(0x42c80000);

            float f = BitConverter.ToSingle(bt1, 0);

            MessageBox.Show(f.ToString());

你可能感兴趣的:(C#,通讯,float,bt,byte,string,c)