c#浮点数和16进制互转

前言:转换的原理此处就不展开了,我可能讲不清楚,只说结论,只说代码怎么写

先放一张截图,是读取的一款流量记的数据,读取工具是modbus pull,需要注意的一点是我圈起来四个16进制数,但每家的顺序不一定都是一样的,有的是高低位调换了的,我们在转换时需要调整至c#能认识的方式,在下发时又要转换位下位机能认识的方式。

在这里插入图片描述

核心代码

			string value = "425F999A";   
            UInt32 x = Convert.ToUInt32(value, 16);
            float fy = BitConverter.ToSingle(BitConverter.GetBytes(x), 0) ;
            Console.WriteLine(fy);
            //浮点数转16进制
            byte[] bytes = BitConverter.GetBytes(fy);
            Console.WriteLine(BitConverter.ToString(bytes)); 

猜你喜欢

转载自blog.csdn.net/weixin_42485732/article/details/125614468
今日推荐