BitConverter.ToDouble() Method in C#


The BitConverter.ToDouble() method in C# is used to return a double-precision floating-point number converted from eight bytes at a specified position in a byte array.

Synchronized

The syntax is as follows −

public static double ToDouble (byte[] val, int begnIndex);

Above, val is the byte array, whereas begnIndex is the beginning position within val.

Example

Let us now see an example −

 Live Demo

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);
      }
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example −

 Live Demo

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);
      }
   }
}

Output

This will produce the following output −

Byte Array = 00-03-07-0A-12-14-19-1A-22
Value = 3
Result = 2.09001158167895E-144
raja
Published on 03-Dec-2019 07:59:16
Advertisements