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
C# BitConverter.ToChar() Method
The BitConverter.ToChar() method in C# converts two consecutive bytes from a byte array into a Unicode character. This method reads two bytes starting at a specified position and interprets them as a 16-bit Unicode character using little-endian byte order.
Syntax
public static char ToChar(byte[] value, int startIndex);
Parameters
-
value − The byte array containing the bytes to convert.
-
startIndex − The starting position within the byte array. Two consecutive bytes are read from this position.
Return Value
Returns a char value formed by combining two bytes at the specified position using little-endian byte order.
Using ToChar() with ASCII Characters
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0x41, 0x00, 0x42, 0x00, 0x43, 0x00 };
Console.WriteLine("Byte Array: {0}", BitConverter.ToString(arr));
for (int i = 0; i < arr.Length - 1; i += 2) {
char result = BitConverter.ToChar(arr, i);
Console.WriteLine("Bytes at position {0}: 0x{1:X2} 0x{2:X2} = '{3}'",
i, arr[i], arr[i + 1], result);
}
}
}
The output of the above code is −
Byte Array: 41-00-42-00-43-00 Bytes at position 0: 0x41 0x00 = 'A' Bytes at position 2: 0x42 0x00 = 'B' Bytes at position 4: 0x43 0x00 = 'C'
Using ToChar() with Unicode Characters
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0x48, 0x00, 0x69, 0x00, 0x21, 0x26 };
Console.WriteLine("Byte Array: {0}", BitConverter.ToString(arr));
for (int i = 0; i < arr.Length - 1; i += 2) {
char result = BitConverter.ToChar(arr, i);
Console.WriteLine("Position {0}: Bytes 0x{1:X2} 0x{2:X2} = '{3}' (Unicode U+{4:X4})",
i, arr[i], arr[i + 1], result, (int)result);
}
}
}
The output of the above code is −
Byte Array: 48-00-69-00-21-26 Position 0: Bytes 0x48 0x00 = 'H' (Unicode U+0048) Position 2: Bytes 0x69 0x00 = 'i' (Unicode U+0069) Position 4: Bytes 0x21 0x26 = '?' (Unicode U+2621)
Handling Exception Cases
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0x41, 0x42, 0x43 };
try {
// This will throw ArgumentException - not enough bytes
char result = BitConverter.ToChar(arr, 2);
Console.WriteLine("Result: " + result);
}
catch (ArgumentException ex) {
Console.WriteLine("Error: " + ex.Message);
}
try {
// This works - reads bytes at index 0 and 1
char result = BitConverter.ToChar(arr, 0);
Console.WriteLine("Valid conversion: '{0}' from bytes 0x{1:X2} 0x{2:X2}",
result, arr[0], arr[1]);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Error: Destination array is not long enough to copy all the items in the collection. Check array index and length. Valid conversion: '?' from bytes 0x41 0x42
Conclusion
The BitConverter.ToChar() method converts two consecutive bytes from a byte array into a Unicode character using little-endian byte order. It requires at least two bytes starting from the specified index and throws an exception if insufficient bytes are available. This method is useful for converting binary data back to character representations.
