C# BitConverter.ToInt64() Method

The BitConverter.ToInt64() method in C# is used to convert eight consecutive bytes from a byte array into a 64-bit signed integer (long). This method is particularly useful when working with binary data or when you need to reconstruct numeric values from byte representations.

Syntax

Following is the syntax for the BitConverter.ToInt64() method −

public static long ToInt64(byte[] value, int startIndex);

Parameters

  • value − An array of bytes containing the eight bytes to convert.

  • startIndex − The starting position within the byte array from which to begin the conversion.

Return Value

Returns a 64-bit signed integer formed by eight bytes beginning at startIndex.

8 Bytes ? 64-bit Long Combined into 64-bit long Byte 0 ... Byte 7

Example 1: Basic Conversion

The following example demonstrates converting 8 bytes from a byte array into a long value −

using System;

public class Demo {
    public static void Main() {
        byte[] arr = { 0, 10, 15, 20, 26, 30, 34, 42, 50 };
        Console.WriteLine("Byte Array = {0}", BitConverter.ToString(arr));
        
        for (int i = 0; i <= arr.Length - 8; i += 8) {
            long res = BitConverter.ToInt64(arr, i);
            Console.WriteLine("\nStarting at index {0}:", i);
            Console.WriteLine("Result = {0}", res);
        }
    }
}

The output of the above code is −

Byte Array = 00-0A-0F-14-1A-1E-22-2A-32

Starting at index 0:
Result = 3036022196155648512

Example 2: Multiple Conversions

This example shows converting multiple 8-byte segments from a larger array −

using System;

public class Demo {
    public static void Main() {
        byte[] arr = { 0, 0, 0, 10, 20, 0, 0, 25, 30, 0, 0, 0, 35, 45, 55, 65, 75 };
        Console.WriteLine("Byte Array = {0}", BitConverter.ToString(arr));
        
        for (int i = 0; i <= arr.Length - 8; i += 8) {
            long res = BitConverter.ToInt64(arr, i);
            Console.WriteLine("\nStarting at index {0}:", i);
            Console.WriteLine("Result = {0}", res);
        }
    }
}

The output of the above code is −

Byte Array = 00-00-00-0A-14-00-00-19-1E-00-00-00-23-2D-37-41-4B

Starting at index 0:
Result = 1801439937015316480

Starting at index 8:
Result = 5423250738304016926

Example 3: Converting Known Values

This example demonstrates converting a known long value to bytes and back −

using System;

public class Demo {
    public static void Main() {
        long originalValue = 123456789012345L;
        Console.WriteLine("Original long value: {0}", originalValue);
        
        byte[] bytes = BitConverter.GetBytes(originalValue);
        Console.WriteLine("As byte array: {0}", BitConverter.ToString(bytes));
        
        long convertedBack = BitConverter.ToInt64(bytes, 0);
        Console.WriteLine("Converted back to long: {0}", convertedBack);
        Console.WriteLine("Values match: {0}", originalValue == convertedBack);
    }
}

The output of the above code is −

Original long value: 123456789012345
As byte array: F9-02-95-FB-07-70-00-00
Converted back to long: 123456789012345
Values match: True

Key Rules

  • The byte array must contain at least 8 bytes starting from the specified index.

  • The method throws an ArgumentException if there are fewer than 8 bytes available.

  • The conversion follows the system's endianness (typically little-endian on most systems).

  • The startIndex parameter must be non-negative and within the array bounds.

Conclusion

The BitConverter.ToInt64() method provides an efficient way to convert 8 consecutive bytes from a byte array into a 64-bit signed integer. This method is essential for binary data processing, file I/O operations, and network communication where numeric data needs to be reconstructed from its byte representation.

Updated on: 2026-03-17T07:04:36+05:30

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements