C#中的BitConverter.ToDouble()方法用于返回双精度浮点数,该双精度浮点数是从字节数组中指定位置的八个字节转换而来的。
语法如下-
public static double ToDouble (byte[] val, int begnIndex);
在上面,val是字节数组,而begnIndex是val中的开始位置。
现在让我们看一个例子-
using System; public class Demo { public static void Main(){ byte[] arr = { 0, 2, 5, 10, 20, 26, 34, 42, 50, 58, 66, 74, 82, 89, 97, 107, 115}; Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr)); for (int i = 1; i < arr.Length - 7; i = i + 8) { double values = BitConverter.ToDouble(arr, i); Console.WriteLine("\nValue = "+arr[i]); Console.WriteLine("Result = "+values); } } }
输出结果
这将产生以下输出-
Byte Array = 00-02-05-0A-14-1A-22-2A-32-3A-42-4A-52-59-61-6B-73 Value = 2 Result = 4.84667324189914E-67 Value = 58 Result = 9.57203245252997E+247
现在让我们来看另一个示例-
using System; public class Demo { public static void Main(){ byte[] arr = { 0, 3, 7, 10, 18, 20, 25, 26, 34}; Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr)); for (int i = 1; i < arr.Length - 7; i = i + 8) { double values = BitConverter.ToDouble(arr, i); Console.WriteLine("\nValue = "+arr[i]); Console.WriteLine("Result = "+values); } } }
输出结果
这将产生以下输出-
Byte Array = 00-03-07-0A-12-14-19-1A-22 Value = 3 Result = 2.09001158167895E-144