C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Static: The BitConverter type contains many static methods, and you do not need to create a new BitConverter to use these.
StaticHere: The byte array is created with 4 values. Each byte is equal to 8 bits, and four bytes is equal to 32 bits, the size of an integer.
Byte ArrayToInt32: The byte array is passed as the first parameter to the ToInt32 and ToUInt32 methods.
And: The second parameter to the methods is an offset parameter. If you are using a larger source array, you can specify the location.
C# program that converts byte array to int
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
Note: The result array size varies based on the argument. This example converts an integer into a four-element byte array.
C# program that converts integer to byte array
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
Info: If you pass an integer type to the GetBytes method, it returns a four-element byte array.
And: If you pass a short to the GetBytes method it returns a two-element byte array.
Array LengthTip: It is helpful to inspect the internals of the BitConverter type with the IL Disassembler tool.
IL DisassemblerTip: This enables you to embed doubles in some kinds of in-memory byte arrays and files.
Main: The BitConverter.GetBytes method is invoked with one argument. The result of the GetBytes method is an eight-element byte array.
Then: The foreach-loop shows what each byte of that array is equal to when displayed in integer form.
ForeachConvert: The final part of the program converts a byte array of eight elements back to a double value.
C# program that gets bytes from double value
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)
Note: 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.
Next: This following list shows the parameter types to GetBytes that are available in the .NET Framework.
OverloadGetBytes overload parameter types
bool, Boolean
char, Char
double, Double
short, Int16
int, Int32
long, Int64
float, Single
ushort, UInt16
uint, UInt32
ulong, UInt64
Example: The ToInt32 method uses the shift << on each byte and the bitwise "|" to combine those shifted values.
ShiftBitwise OrTip: This would improve performance and reduce footprint for fairly simple data models.
And: Byte arrays are the lowest-level representation of memory that is practical in the C# language.
Note: More information about endianness is available at Wikipedia. This next program is useless.
Endianness: WikipediaC# program that uses IsLittleEndian
using System;
class Program
{
static void Main()
{
Console.WriteLine(BitConverter.IsLittleEndian);
}
}
Output
True