本文介绍了在c#中将字节数组转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

float input = 25;
            byte[] buffer = BitConverter.GetBytes(input);



缓冲区包含0x00 0x00 0xc8 0x41。我需要将它转换回浮点数,即25.我只知道十六进制值,请告诉我怎么做。


buffer contains 0x00 0x00 0xc8 0x41.I need to convert it back to float i.e 25.I knows only hex values,Please tell how to do it.

推荐答案

float input = 25;
byte[] buffer = BitConverter.GetBytes(input);
float output = BitConverter.ToSingle(buffer,0);



private Single ConvertHexToSingle(string hexVal)
       {

           try
           {

               int i = 0, j = 0;

               byte[] bArray = new byte[4];

               for (i = 0; i <= hexVal.Length - 1; i += 2)
               {

                   bArray[j] = Byte.Parse(hexVal[i].ToString() + hexVal[i + 1].ToString(), System.Globalization.NumberStyles.HexNumber);

                   j += 1;

               }

               Array.Reverse(bArray);

               Single s = BitConverter.ToSingle(bArray, 0);

               return (s);

           }
           catch (Exception ex)
           {

               throw new FormatException("The supplied hex value is either empty or in an incorrect format.  Use the " +

                   "following format: 00000000", ex);

           }

       }


这篇关于在c#中将字节数组转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 00:28
查看更多