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.ToUInt32() Method
The BitConverter.ToUInt32() method in C# converts four consecutive bytes from a byte array into a 32-bit unsigned integer. This method reads bytes in little-endian format, where the least significant byte comes first.
Syntax
public static uint ToUInt32(byte[] value, int startIndex);
Parameters
value − The byte array containing the data to convert.
startIndex − The starting position within the byte array (must be between 0 and array length minus 4).
Return Value
Returns a 32-bit unsigned integer (uint) formed by four bytes beginning at startIndex.
Using BitConverter.ToUInt32() with Different Start Positions
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = {0, 3, 5, 10, 15, 2, 8, 12};
Console.WriteLine("Byte Array: " + BitConverter.ToString(arr));
Console.WriteLine();
// Convert 4 bytes starting from index 0
uint result1 = BitConverter.ToUInt32(arr, 0);
Console.WriteLine("Bytes at index 0-3: " + BitConverter.ToString(arr, 0, 4));
Console.WriteLine("ToUInt32(arr, 0) = " + result1);
Console.WriteLine();
// Convert 4 bytes starting from index 2
uint result2 = BitConverter.ToUInt32(arr, 2);
Console.WriteLine("Bytes at index 2-5: " + BitConverter.ToString(arr, 2, 4));
Console.WriteLine("ToUInt32(arr, 2) = " + result2);
Console.WriteLine();
// Convert 4 bytes starting from index 4
uint result3 = BitConverter.ToUInt32(arr, 4);
Console.WriteLine("Bytes at index 4-7: " + BitConverter.ToString(arr, 4, 4));
Console.WriteLine("ToUInt32(arr, 4) = " + result3);
}
}
The output of the above code is −
Byte Array: 00-03-05-0A-0F-02-08-0C Bytes at index 0-3: 00-03-05-0A ToUInt32(arr, 0) = 167837440 Bytes at index 2-5: 05-0A-0F-02 ToUInt32(arr, 2) = 33951237 Bytes at index 4-7: 0F-02-08-0C ToUInt32(arr, 4) = 201458191
Converting Known Values to UInt32
Example
using System;
public class Demo {
public static void Main() {
// Create byte array representing specific uint values
uint originalValue = 1000000;
byte[] bytes = BitConverter.GetBytes(originalValue);
Console.WriteLine("Original uint value: " + originalValue);
Console.WriteLine("Converted to bytes: " + BitConverter.ToString(bytes));
// Convert back to uint
uint convertedBack = BitConverter.ToUInt32(bytes, 0);
Console.WriteLine("Converted back to uint: " + convertedBack);
Console.WriteLine("Values match: " + (originalValue == convertedBack));
Console.WriteLine();
// Example with maximum uint value
uint maxValue = uint.MaxValue;
byte[] maxBytes = BitConverter.GetBytes(maxValue);
Console.WriteLine("Max uint value: " + maxValue);
Console.WriteLine("Max value as bytes: " + BitConverter.ToString(maxBytes));
Console.WriteLine("Converted back: " + BitConverter.ToUInt32(maxBytes, 0));
}
}
The output of the above code is −
Original uint value: 1000000 Converted to bytes: 40-42-0F-00 Converted back to uint: 1000000 Values match: True Max uint value: 4294967295 Max value as bytes: FF-FF-FF-FF Converted back: 4294967295
How It Works
The method combines four consecutive bytes using little-endian byte order. The first byte becomes the least significant byte, and the fourth byte becomes the most significant byte. The formula is:
result = byte[0] + (byte[1] << 8) + (byte[2] << 16) + (byte[3] << 24)
Common Use Cases
File I/O − Reading binary data from files where integers are stored as byte sequences.
Network Communication − Converting received byte packets back to integer values.
Data Serialization − Reconstructing objects from byte arrays.
Memory Operations − Working with raw memory data in unsafe contexts.
Conclusion
The BitConverter.ToUInt32() method provides an efficient way to convert four consecutive bytes into a 32-bit unsigned integer using little-endian byte order. It's essential for binary data processing, file operations, and network communication where data is transmitted as byte sequences.
