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.ToUInt16() Method
The BitConverter.ToUInt16() method in C# converts two consecutive bytes from a byte array into a 16-bit unsigned integer (ushort). This method is particularly useful when working with binary data, network protocols, or file formats that store numeric values as byte sequences.
Syntax
public static ushort ToUInt16(byte[] value, int startIndex);
Parameters
value: The byte array containing the bytes to convert.
startIndex: The starting position within the byte array (must have at least 2 bytes remaining).
Return Value
Returns a 16-bit unsigned integer (ushort) formed by combining two bytes from the specified position. The method uses little-endian byte order, meaning the byte at startIndex becomes the least significant byte.
Example 1: Basic Conversion
using System;
public class Demo {
public static void Main() {
byte[] arr = { 10, 20, 30, 40, 50 };
Console.WriteLine("Byte Array: " + BitConverter.ToString(arr));
for (int i = 0; i <= arr.Length - 2; i += 2) {
ushort result = BitConverter.ToUInt16(arr, i);
Console.WriteLine($"Bytes [{i}],[{i+1}]: {arr[i]},{arr[i+1]} ? UInt16: {result}");
}
}
}
The output of the above code is −
Byte Array: 0A-14-1E-28-32 Bytes [0],[1]: 10,20 ? UInt16: 5130 Bytes [2],[3]: 30,40 ? UInt16: 10270
Example 2: Converting Multiple Pairs
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0, 1, 255, 255, 100, 200 };
Console.WriteLine("Byte Array: " + BitConverter.ToString(arr));
Console.WriteLine("\nConverting byte pairs to UInt16:");
for (int i = 0; i <= arr.Length - 2; i += 2) {
ushort result = BitConverter.ToUInt16(arr, i);
Console.WriteLine($"Position {i}: [{arr[i]:X2},{arr[i+1]:X2}] = {result}");
}
}
}
The output of the above code is −
Byte Array: 00-01-FF-FF-64-C8 Converting byte pairs to UInt16: Position 0: [00,01] = 256 Position 2: [FF,FF] = 65535 Position 4: [64,C8] = 51300
How It Works
The method combines two bytes using little-endian byte order:
Formula:
result = byte[startIndex] + (byte[startIndex + 1] × 256)The first byte becomes the least significant byte (LSB)
The second byte becomes the most significant byte (MSB)
Range: 0 to 65,535 (0x0000 to 0xFFFF)
Common Use Cases
Binary file parsing: Reading numeric data from files
Network protocols: Converting received byte streams to integers
Data serialization: Reconstructing numeric values from byte arrays
Hardware communication: Processing sensor data or device responses
Conclusion
The BitConverter.ToUInt16() method efficiently converts two consecutive bytes into a 16-bit unsigned integer using little-endian byte order. This method is essential for binary data processing, file parsing, and network communication scenarios where byte arrays need to be converted back to numeric values.
