BitConverter class in C# is used to convert base data types to an array of bytes and vice versa. This class is defined under the System namespace and provides various methods to perform conversions efficiently. It helps in manipulating value types in their raw form, representing them as a sequence of bytes.
Endianness: A key concept when working with BitConverter is endianness, which defines the byte order in memory:
- Little-endian (least significant byte stored first)
- Big-endian (most significant byte stored first)
The IsLittleEndian property indicates the endianness of the system:
// C# program to check system endianness
using System;
class Geeks
{
static void Main()
{
Console.WriteLine("Is system little-endian? "
+ BitConverter.IsLittleEndian);
}
}
Output
Is system little-endian? True
Example: Converting Integer to ByteArray and vice-versa using BitCoverter Class.
// C# program to convert an integer to a
// byte array and back using BitConverter
using System;
class Geeks
{
static void Main()
{
int num = 357;
// Convert integer to byte array
byte[] byteArray = BitConverter.GetBytes(num);
// Print byte array as a hexadecimal string
Console.WriteLine("Byte Array: "
+ BitConverter.ToString(byteArray));
// Convert byte array back to integer
int res = BitConverter.ToInt32(byteArray, 0);
Console.WriteLine("Converted Integer: " + res);
}
}
Output
Byte Array: 65-01-00-00 Converted Integer: 357
Methods
Method | Description |
|---|---|
Converts the specified double-precision floating point number to a 64-bit signed integer. | |
GetBytes() | Converts the specified data to an array of bytes. |
Int32BitsToSingle() | Converts the specified 32-bit signed integer to a single-precision floating point number. |
Converts the specified 64-bit signed integer to a double-precision floating point number. | |
SingleToInt32Bits() | Converts the specified single-precision floating point number to a 32-bit signed integer. |
Returns a Boolean value converted from the byte at a specified position in a byte array. | |
Returns a Unicode character converted from two bytes at a specified position in a byte array. | |
Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array. | |
Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array. | |
Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array. | |
Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array. | |
Returns a single-precision floating point number converted from four bytes at a specified position in a byte array. | |
Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation. | |
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array. | |
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array. | |
| Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array. |
Example: Converting Long Integer to Byte Array
// C# program to convert long integers
// to byte arrays using BitConverter
using System;
class Geeks
{
static public void Main()
{
// Initialize an array of long integers
long[] e = { long.MaxValue, long.MinValue, 1000000000, 0xDDDDD };
// Display the elements and their byte arrays
Console.WriteLine("Elements and their byte arrays:");
foreach (long num in e)
{
byte[] byteArr = BitConverter.GetBytes(num);
Console.WriteLine($"{num} => {BitConverter.ToString(byteArr)}");
}
}
}
Output
Elements and their byte arrays: 9223372036854775807 => FF-FF-FF-FF-FF-FF-FF-7F -9223372036854775808 => 00-00-00-00-00-00-00-80 1000000000 => 00-CA-9A-3B-00-00-00-00 908765 => DD-DD-0D-00-00-00-00-00
Example: Converting 64-bit Integer to Double
// C# program to convert a 64-bit integer
// to a double using BitConverter
using System;
class Geeks
{
static public void Main()
{
long e = 0xFFFFFFFFFFFFFFF;
// Convert the 64-bit signed integer to a double
double res = BitConverter.Int64BitsToDouble(e);
Console.WriteLine("Converted Element is : {0}", res);
}
}
Output
Converted Element is : 1.28822975391943E-231