IEE754算法

C# IEE754算法,仪表设备经常用的数据指令.16进制转10进制,10进制转16进制互转.留作备份,并发给刚入门工控的人.
算法原理很多人都解释啦,在此就不多说啦,时间有限,上来就干!

  public static class IEE754Method
    {
        public static float HexToFloat(this string hexValue)
        {
            try
            {
                uint uintValue = Convert.ToUInt32(hexValue, 16);
                byte[] bytesValue = BitConverter.GetBytes(uintValue);
                float floatValue = BitConverter.ToSingle(bytesValue, 0);
                return floatValue;
            }
            catch
            {
                throw;
            }
        }

        public static string FloatToHex(this float floatValue)
        {
            try
            {
                byte[] bytesValue = BitConverter.GetBytes(floatValue);
                int intValue = BitConverter.ToInt32(bytesValue, 0);
                return intValue.ToString("X8");
            }
            catch
            {
                throw;
            }
        }
    }

 

posted @ 2019-01-29 15:05  Mr.Mengs  阅读(845)  评论(0编辑  收藏  举报