Let's go

进制转换(进阶)

一、16进制转float型

        static void Main(string[] args)
        {
            string value = "B6798842";
            value = "428879B6";

            #region Demo1
            UInt32 x = Convert.ToUInt32(value, 16);//将字符串转为16进制32位无符号整数
            float fy = BitConverter.ToSingle(BitConverter.GetBytes(x), 0);
            Console.WriteLine("Demo1==>" + fy);
            #endregion

            #region Demo2
            byte[] bytes = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                bytes[i] = Convert.ToByte(value.Substring((3 - i) * 2, 2), 16);
            }
            float fy2 = BitConverter.ToSingle(bytes, 0);
            Console.WriteLine("Demo2==>" + fy2);
            #endregion

            #region Demo3
            value = "B6798842";
            byte[] bytes3 = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                bytes3[i] = Convert.ToByte(value.Substring(i * 2, 2), 16);
            }
            float fy3 = BitConverter.ToSingle(bytes3, 0);
            Console.WriteLine("Demo3==>" + fy3);
            #endregion

            Console.ReadLine();
        }
View Code

其他

posted @ 2023-03-24 10:50  chenze  阅读(14)  评论(0编辑  收藏  举报
有事您Q我