
BitConverter converts representations. It changes a range of bytes to a different value type such as an integer or double. This type contains utility methods. It is useful when manipulating or reading binary data.
This C# page gives examples of the BitConverter class. BitConverter changes representations of types.

This program shows how you can use the BitConverter class and its static methods ToInt32 and ToUInt32 to convert the byte values stores in a byte array to native integers. The BitConverter type contains many static methods, and you do not need to create a new BitConverter() to use these. Instead, you can use the composite name such as BitConverter.GetBytes to call these public static methods.
Int Type Uint ExampleTip: One piece of data may be represented in many different ways, yet at a high level be equivalent.
Program that converts byte array to int [C#]
using System;
class Program
{
static void Main()
{
//
// Create an array of four bytes.
// ... Then convert it into an integer and unsigned integer.
//
byte[] array = new byte[4];
array[0] = 1; // Lowest
array[1] = 64;
array[2] = 0;
array[3] = 0; // Sign bit
//
// Use BitConverter to convert the bytes to an int and a uint.
// ... The int and uint can have different values if the sign bit differs.
//
int result1 = BitConverter.ToInt32(array, 0); // Start at first index
uint result2 = BitConverter.ToUInt32(array, 0); // First index
Console.WriteLine(result1);
Console.WriteLine(result2);
}
}
Output
16385
16385
Overview. The byte[] array is allocated and populated with four arbitrary integer representations of byte values. Each byte is equal to 8 bits, and four bytes is equal to 32 bits, the size of an integer. The byte[] array is passed as the first parameter to the ToInt32 and ToUInt32 methods. The second parameter to the methods is an offset parameter; if you are using a larger source array, you can specify the location.
Sign bit and byte representations. The program demonstrates converting a byte[] array to an integer and an unsigned integer. The difference between integers and unsigned integers is that the signed integer reserves a special sign bit. If you change the value you assign to the final element in the byte array in the example, you can see how the int and the uint will have different values because of their varying treatment of the sign bit.

The BitConverter.GetBytes method group contains many static methods, differentiated only by their parameter. The GetBytes method in this example receives a value of type integer, but the other overloads can be used in the same way. The resulting array has a size that varies based on the parameter type. This example converts an integer into a four-element byte array.
Program that converts integer to byte array [C#]
using System;
class Program
{
static void Main()
{
//
// Converts an integer constant into an array of bytes.
// ... This example allocates a new array on the managed heap.
//
int value = 16385;
byte[] array = BitConverter.GetBytes(value);
Console.WriteLine(array[0]);
Console.WriteLine(array[1]);
Console.WriteLine(array[2]);
Console.WriteLine(array[3]);
}
}
Output
1
64
0
0
Main. The program invokes the BitConverter.GetBytes static method in the Main entry point. The BitConverter.GetBytes method is a method group that contains several overloads, each accepting a different value type parameter. It returns a newly allocated byte array and you can assign this array reference to a local variable, as shown in the program.
Returned array. The length of the return array is dependent on the overload you invoke in your program. In other words, if you pass an integer type to the GetBytes method, it will return a four element byte[] array. If you pass a short integer to the GetBytes method it will return a two element byte array. It is helpful to inspect the internals of the BitConverter type with the IL Disassembler tool.
IL Disassembler Tutorial
Continuing on, this program demonstrates how you can take an arbitrary double variable with any value in its storage location and convert it to an array of bytes that are exactly equivalent in a logical sense to its original value. The byte array can then be persisted to the disk and later converted back to a double. This enables you to embed doubles in some kinds of in-memory byte arrays and files.
Double TypeProgram that gets bytes from double value [C#]
using System;
class Program
{
static void Main()
{
//
// Use any double value.
//
double value = 5000.1234;
//
// Invoke BitConverter.GetBytes to convert double to bytes.
//
byte[] array = BitConverter.GetBytes(value);
foreach (byte element in array)
{
Console.WriteLine(element);
}
//
// You can convert the bytes back to a double.
//
double result = BitConverter.ToDouble(array, 0);
Console.WriteLine(result);
}
}
Output
84 (Eight bytes)
116
36
151
31
136
179
64
5000.1234 (Double value)Main. The double variable is assigned an arbitrary value in the Main entry point method body. The BitConverter.GetBytes method from the system namespace is invoked with one argument. The result of the GetBytes method is an eight element byte array. The foreach loop shows what each byte of that array is equal to when displayed in integer form.
Foreach Loop Examples
Converting to double. The final part of the program text demonstrates how you can convert a byte[] array of eight elements back to a double value. Note that you can use any byte array, not just ones you just acquired in the program text. You can even use a byte array representation embedded in the source text. The ToDouble method proves that the eight bytes are equal to the correct double value.
Note: The BitConverter.GetBytes method can be used with a double value argument in the C# parameter. We proved that the byte array representation of the double value is exactly equivalent to the double value itself. This technique can be used as an optimization on in-memory data or files to enable encoding of byte representations and control structures in files.
There are overloads available in the BitConverter.GetBytes method group in the .NET Framework. The C# compiler will automatically infer which overload you want based on the parameter type in the method call. Note that if you want to get four bytes from a two byte value, you can always cast the parameter type to an integer before calling GetBytes. This following list shows the parameter types to GetBytes that are available in the .NET Framework.
Overload MethodGetBytes overload parameter types bool, Boolean char, Char double, Double short, Int16 int, Int32 long, Int64 float, Single ushort, UInt16 uint, UInt32 ulong, UInt64

The BitConverter class is available in all CLI languages, including VB.NET. The BitConverter methods use bitwise operators to read from byte arrays and return value types such as integers. For example, the ToInt32 method uses the shift << on each byte and the bitwise | to combine those shifted values.
The BitConverter contains a lot of special-case logic, which will reduce performance if you do not need those conditionals. The GetBytes overloads use unsafe pointer operations to improve performance.

The BitConverter type provides a higher-level way to manipulate byte representations. It is useful for manipulating binary file formats. For example, you can encode a huge amount of data into a byte array or byte stream, which would greatly improve performance and reduce footprint for fairly simple data models. Byte arrays are the lowest-level representation of memory that is practical in the C# language, as the byte is the minimum addressable memory unit on modern systems.
Byte Array
The BitConverter type—found in the C# programming language and .NET Framework—is used to convert data represented in a byte array to different value type representations. The BitConverter offers methods such as ToInt32 to convert arrays of bytes to an integer. Additionally we find many overloads of GetBytes—these convert a value such as an integer to an array of bytes.
Cast Examples