Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. This method interprets 8 consecutive bytes as a 64-bit IEEE 754 floating-point number.
Syntax
The syntax is as follows −
public static double ToDouble(byte[] value, int startIndex);
Parameters
-
value − An array of bytes containing the 8 bytes to convert.
-
startIndex − The starting position within the byte array from which to begin reading the 8 bytes.
Return Value
Returns a double formed by 8 bytes beginning at startIndex.
How It Works
The method reads exactly 8 consecutive bytes from the array starting at the specified index and interprets them as the binary representation of a double-precision floating-point number. The byte order depends on the system's endianness.
Example Using Known Double Values
This example demonstrates converting known double values to bytes and back to doubles −
using System;
public class Demo {
public static void Main() {
double originalValue = 123.456;
Console.WriteLine("Original double: " + originalValue);
// Convert double to byte array
byte[] bytes = BitConverter.GetBytes(originalValue);
Console.WriteLine("Byte array: " + BitConverter.ToString(bytes));
// Convert back to double
double convertedValue = BitConverter.ToDouble(bytes, 0);
Console.WriteLine("Converted back: " + convertedValue);
Console.WriteLine("Values match: " + (originalValue == convertedValue));
}
}
The output of the above code is −
Original double: 123.456 Byte array: 77-BE-9F-1A-2F-DD-5E-40 Converted back: 123.456 Values match: True
Example with Multiple Double Values
This example shows how to extract multiple double values from a byte array −
using System;
public class Demo {
public static void Main() {
// Create byte array from multiple double values
double[] originalDoubles = { 1.23, 4.56, 7.89 };
byte[] combinedBytes = new byte[24]; // 3 doubles × 8 bytes each
// Store doubles as bytes
for (int i = 0; i
The output of the above code is −
Combined byte array: AE-47-E1-7A-14-AE-F3-3F-83-C0-CA-A1-45-B6-12-40-F6-28-5C-8F-C2-F5-1F-40
Double at position 0: 1.23
Double at position 8: 4.56
Double at position 16: 7.89
Example Handling ArgumentException
This example demonstrates proper bounds checking to avoid exceptions −
using System;
public class Demo {
public static void Main() {
byte[] smallArray = { 1, 2, 3, 4, 5 };
Console.WriteLine("Array length: " + smallArray.Length);
Console.WriteLine("Need 8 bytes for double conversion");
if (smallArray.Length >= 8) {
double result = BitConverter.ToDouble(smallArray, 0);
Console.WriteLine("Result: " + result);
} else {
Console.WriteLine("Array too small - need at least 8 bytes");
}
// Demonstrate with sufficient bytes
byte[] sufficientArray = new byte[10];
for (int i = 0; i
The output of the above code is −
Array length: 5
Need 8 bytes for double conversion
Array too small - need at least 8 bytes
Valid conversion result: 5.447603722011605E-270
Conclusion
The BitConverter.ToDouble() method converts 8 consecutive bytes from a byte array into a double-precision floating-point number using IEEE 754 format. Always ensure your byte array has at least 8 bytes available from the starting index to avoid ArgumentException.
