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());
本文介绍了如何在串口通讯中进行浮点型(float)与整形(4字节)之间的转换。通过BitConverter类,将浮点数转换为字节数组,再将字节数组转换为整数,然后以16进制字符串形式展示。同时,演示了逆转换过程,从4字节的16进制数值还原回浮点数。
2万+

被折叠的 条评论
为什么被折叠?



