Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
BitConverter Class in C#
The BitConverter class in C# provides static methods to convert base data types to byte arrays and vice versa. It handles the conversion between different data types and their byte representations, making it essential for low-level data manipulation, file I/O, and network communication.
Key Methods
| Method | Description |
|---|---|
| GetBytes(Boolean) | Returns the specified Boolean value as a byte array. |
| GetBytes(Char) | Returns the specified Unicode character value as an array of bytes. |
| GetBytes(Double) | Returns the specified double-precision floating-point value as an array of bytes. |
| GetBytes(Int32) | Returns the specified 32-bit signed integer value as an array of bytes. |
| ToBoolean(Byte[], Int32) | Returns a Boolean value converted from the byte at a specified position in a byte array. |
| ToInt32(Byte[], Int32) | Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array. |
| ToString(Byte[]) | Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation. |
Converting Data Types to Bytes
The GetBytes() method converts various data types to their byte array representation −
Example
using System;
public class Demo {
public static void Main() {
int number = 12345;
double value = 3.14159;
bool flag = true;
byte[] intBytes = BitConverter.GetBytes(number);
byte[] doubleBytes = BitConverter.GetBytes(value);
byte[] boolBytes = BitConverter.GetBytes(flag);
Console.WriteLine("Integer 12345 as bytes:");
foreach (byte b in intBytes) {
Console.Write(b + " ");
}
Console.WriteLine("\nDouble 3.14159 as bytes:");
foreach (byte b in doubleBytes) {
Console.Write(b + " ");
}
Console.WriteLine("\nBoolean true as bytes:");
foreach (byte b in boolBytes) {
Console.Write(b + " ");
}
}
}
The output of the above code is −
Integer 12345 as bytes: 57 48 0 0 Double 3.14159 as bytes: 110 18 131 192 202 33 9 64 Boolean true as bytes: 1
Converting Bytes to Data Types
The ToBoolean(), ToInt32(), and similar methods convert byte arrays back to their original data types −
Syntax
public static bool ToBoolean(byte[] arr, int startIndex); public static int ToInt32(byte[] arr, int startIndex);
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = { 50, 100, 0, 0 };
Console.WriteLine("Array values:");
for (int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + " ");
}
Console.WriteLine("\nConverted values:");
for (int index = 0; index < arr.Length; index++) {
bool res = BitConverter.ToBoolean(arr, index);
Console.WriteLine("Index " + index + ": " + res);
}
int intValue = BitConverter.ToInt32(arr, 0);
Console.WriteLine("As Int32: " + intValue);
}
}
The output of the above code is −
Array values: 50 100 0 0 Converted values: Index 0: True Index 1: True Index 2: False Index 3: False As Int32: 25650
Converting to Hexadecimal String
The ToString() method converts byte arrays to hexadecimal string representations −
Example
using System;
public class Demo {
public static void Main() {
double d = 5.646587687;
Console.WriteLine("Value = " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.WriteLine("64-bit signed integer = " + res);
byte[] bytes = BitConverter.GetBytes(d);
string hexString = BitConverter.ToString(bytes);
Console.WriteLine("Hexadecimal representation: " + hexString);
string hexSubset = BitConverter.ToString(bytes, 2, 4);
Console.WriteLine("Bytes 2-5 as hex: " + hexSubset);
}
}
The output of the above code is −
Value = 5.646587687 64-bit signed integer = 4618043510978159912 Hexadecimal representation: 98-EE-90-1F-85-46-16-40 Bytes 2-5 as hex: 90-1F-85-46
Common Use Cases
-
File I/O operations − Converting data types to bytes for binary file storage.
-
Network communication − Serializing data for transmission over networks.
-
Cryptography − Converting data to byte arrays for encryption algorithms.
-
Memory manipulation − Low-level operations requiring byte-level access to data.
Conclusion
The BitConverter class in C# provides essential functionality for converting between data types and their byte representations. It enables efficient data serialization, file operations, and low-level memory manipulation by handling the conversion between various data types and byte arrays.
