BitConverter
This type converts representations—it changes a range of bytes to a different value type such as an int
or double
. It contains utility methods.
Consider a byte
array—it may contain integers, or other data that is composed of bytes. It has many bytes to a single value. BitConverter
helps here.
We use the BitConverter
class
and ToInt32
and ToUInt32
. These methods convert the byte
values stores in a byte
array to native integers.
BitConverter
type contains many static
methods, and you do not need to create a new BitConverter
to use these.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
array is passed as the first parameter to the ToInt32
and ToUInt32
methods.using System; // // 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); uint result2 = BitConverter.ToUInt32(array, 0); Console.WriteLine(result1); Console.WriteLine(result2);16385 16385
GetBytes
Here we invoke GetBytes
. In this program, GetBytes
receives a value of type integer. The other overloads can be used in the same way.
byte
array.GetBytes()
returns a newly-allocated byte
array and you can assign this array reference to a local variable.GetBytes
method, it returns a four-element byte
array.short
to the GetBytes
method it returns a two-element byte
array.using System; // // 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]);1 64 0 0
ToDouble
Continuing on, this code receives a double
. It converts it to an array of bytes that are exactly equivalent in a logical sense to its original value.
byte
arrays and files.BitConverter.GetBytes
method is invoked with one argument. The result of the GetBytes
method is an eight-element byte
array.foreach
-loop shows what each byte
of that array is equal to when displayed in integer form.byte
array of eight elements back to a double
value.using System; // // 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);84 (Eight bytes) 116 36 151 31 136 179 64 5000.1234 (Double value)
IsLittleEndian
The BitConverter
has a public static
IsLittleEndian
property. Most personal computers are little endian. Endianness refers to how bits are ordered in words.
using System; Console.WriteLine(BitConverter.IsLittleEndian);True
GetBytes
has many overloads. The C# compiler will automatically infer which overload you want based on the parameter type in the method call.
byte
value, you can always cast the parameter type to an integer before calling GetBytes
.bool, Boolean char, Char double, Double short, Int16 int, Int32 long, Int64 float, Single ushort, UInt16 uint, UInt32 ulong, UInt64
How does BitConverter
work internally? The BitConverter
methods use bitwise operators to read from byte
arrays and return value types such as integers.
ToInt32
method uses the shift on each byte
and the bitwise "|" to combine those shifted values.BitConverter
contains a lot of special-case logic, which will reduce performance if you do not need those conditionals.BitConverter
provides a higher-level way to manipulate byte
representations. It is useful for manipulating binary file formats.
byte
array or byte
stream.The BitConverter
type is used to convert data represented in a byte
array to different value type representations. Methods (like ToInt32
) convert arrays of bytes.