BitConverter Examples
This page was last reviewed on May 24, 2023.
Dot Net Perls
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.
Some uses. 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.
First example. We use the BitConverter class and ToInt32 and ToUInt32. These methods convert the byte values stores in a byte array to native integers.
int, uint
Info The BitConverter type contains many static methods, and you do not need to create a new BitConverter to use these.
Here 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 Array
Argument 1 The byte array is passed as the first parameter to the ToInt32 and ToUInt32 methods.
Argument 2 The second parameter to the methods is an offset parameter. If you are using a larger source array, you can specify the location.
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.
Note The result array size varies based on the argument. This example converts an integer into a four-element byte array.
Return GetBytes() returns a newly-allocated byte array and you can assign this array reference to a local variable.
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 Length
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.
Tip This enables you to embed doubles in some kinds of in-memory byte arrays and files.
Detail 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.
Detail The final part of the program converts a 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);
Overloads. GetBytes has many overloads. The C# compiler will automatically infer which overload you want based on the parameter type in the method call.
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.
bool, Boolean char, Char double, Double short, Int16 int, Int32 long, Int64 float, Single ushort, UInt16 uint, UInt32 ulong, UInt64
Internals. How does BitConverter work internally? The BitConverter methods use bitwise operators to read from byte arrays and return value types such as integers.
Detail The ToInt32 method uses the shift on each byte and the bitwise "|" to combine those shifted values.
Bitwise Shift
Bitwise Or
Important The BitConverter contains a lot of special-case logic, which will reduce performance if you do not need those conditionals.
Discussion. BitConverter provides a higher-level way to manipulate byte representations. It is useful for manipulating binary file formats.
Info For example, you can encode a huge amount of data into a byte array or byte stream.
Tip This would improve performance and reduce footprint for fairly simple data models.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 24, 2023 (simplify).
© 2007-2024 Sam Allen.