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
C# BitConverter.ToUInt64 Method
The BitConverter.ToUInt64() method in C# converts eight consecutive bytes from a byte array into a 64-bit unsigned integer (ulong). This method is useful when you need to reconstruct numeric values from byte data, such as reading from binary files or network streams.
Syntax
public static ulong ToUInt64(byte[] value, int startIndex);
Parameters
- value − The byte array containing the bytes to convert.
- startIndex − The starting position within the byte array (must have at least 8 bytes from this position).
Return Value
Returns a 64-bit unsigned integer (ulong) formed by eight bytes beginning at startIndex. The conversion follows little-endian byte order, where the least significant byte comes first.
Using BitConverter.ToUInt64 with Basic Conversion
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0, 0, 1, 3, 5, 7, 9, 11, 15 };
Console.WriteLine("Byte Array:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine($"Index {i}: {arr[i]}");
}
Console.WriteLine($"\nByte Array (Hex): {BitConverter.ToString(arr)}");
// Convert 8 bytes starting at index 1
if (arr.Length >= 9) { // Need at least 8 bytes from index 1
ulong result = BitConverter.ToUInt64(arr, 1);
Console.WriteLine($"\nConverting 8 bytes from index 1:");
Console.WriteLine($"Result: {result}");
}
}
}
The output of the above code is −
Byte Array: Index 0: 0 Index 1: 0 Index 2: 1 Index 3: 3 Index 4: 5 Index 5: 7 Index 6: 9 Index 7: 11 Index 8: 15 Byte Array (Hex): 00-00-01-03-05-07-09-0B-0F Converting 8 bytes from index 1: Result: 1083970061066240256
Using BitConverter.ToUInt64 with Multiple Conversions
using System;
public class Demo {
public static void Main() {
byte[] data = {
0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, // First 8 bytes
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 // Second 8 bytes
};
Console.WriteLine($"Full array (Hex): {BitConverter.ToString(data)}");
// Convert first 8 bytes (index 0-7)
ulong value1 = BitConverter.ToUInt64(data, 0);
Console.WriteLine($"\nBytes 0-7 as UInt64: {value1}");
// Convert next 8 bytes (index 8-15)
ulong value2 = BitConverter.ToUInt64(data, 8);
Console.WriteLine($"Bytes 8-15 as UInt64: {value2}");
// Show individual byte contributions
Console.WriteLine($"\nFirst conversion breakdown:");
for (int i = 0; i < 8; i++) {
Console.WriteLine($"Byte {i}: 0x{data[i]:X2} (position {i * 8} in result)");
}
}
}
The output of the above code is −
Full array (Hex): 10-20-30-40-50-60-70-80-01-02-03-04-05-06-07-08 Bytes 0-7 as UInt64: 9255003132036915216 Bytes 8-15 as UInt64: 578721382704613377 First conversion breakdown: Byte 0: 0x10 (position 0 in result) Byte 1: 0x20 (position 8 in result) Byte 2: 0x30 (position 16 in result) Byte 3: 0x40 (position 24 in result) Byte 4: 0x50 (position 32 in result) Byte 5: 0x60 (position 40 in result) Byte 6: 0x70 (position 48 in result) Byte 7: 0x80 (position 56 in result)
Using BitConverter.ToUInt64 with Error Handling
using System;
public class Demo {
public static void Main() {
byte[] smallArray = { 1, 2, 3, 4, 5 }; // Only 5 bytes
byte[] validArray = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
try {
// This will throw an exception - not enough bytes
ulong result1 = BitConverter.ToUInt64(smallArray, 0);
Console.WriteLine($"Small array result: {result1}");
}
catch (ArgumentException ex) {
Console.WriteLine($"Error with small array: {ex.Message}");
}
try {
// This works - exactly 8 bytes of 0xFF
ulong result2 = BitConverter.ToUInt64(validArray, 0);
Console.WriteLine($"Valid array result: {result2}");
Console.WriteLine($"Max UInt64 value: {ulong.MaxValue}");
}
catch (ArgumentException ex) {
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
}
The output of the above code is −
Error with small array: Destination array is not long enough to copy all the items in the collection. Check array index and length. Valid array result: 18446744073709551615 Max UInt64 value: 18446744073709551615
Conclusion
The BitConverter.ToUInt64() method efficiently converts 8 consecutive bytes from a byte array into a 64-bit unsigned integer using little-endian byte order. Remember to ensure your byte array has at least 8 bytes available from the specified start index to avoid exceptions.
