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.ToInt32() Method in C#
The BitConverter.ToInt32() method in C# is used to convert four bytes from a byte array at a specified position into a 32-bit signed integer. This method reads four consecutive bytes and interprets them as an integer value according to the system's endianness (little-endian on most systems).
Syntax
Following is the syntax for the BitConverter.ToInt32() method −
public static int ToInt32(byte[] value, int startIndex);
Parameters
- value − A byte array containing the bytes to convert.
- startIndex − The starting position within the value array (zero-based index).
Return Value
Returns a 32-bit signed integer formed by four bytes beginning at startIndex.
How It Works
The method combines four consecutive bytes into a single integer. On little-endian systems (most common), the bytes are read in reverse order, where the first byte becomes the least significant byte of the resulting integer.
Example with Basic Conversion
Here's a simple example demonstrating the conversion −
using System;
public class Demo {
public static void Main() {
byte[] arr = { 10, 20, 30, 40, 50 };
Console.WriteLine("Byte Array = {0}", BitConverter.ToString(arr));
// Convert 4 bytes starting at index 1 to int32
int result = BitConverter.ToInt32(arr, 1);
Console.WriteLine("Bytes at index 1-4: {0}, {1}, {2}, {3}", arr[1], arr[2], arr[3], arr[4]);
Console.WriteLine("Converted to Int32: {0}", result);
}
}
The output of the above code is −
Byte Array = 0A-14-1E-28-32 Bytes at index 1-4: 20, 30, 40, 50 Converted to Int32: 841489940
Example with Multiple Conversions
This example shows converting multiple sets of four bytes −
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0, 0, 10, 15, 20, 26, 32, 34, 40 };
Console.WriteLine("Byte Array = {0}", BitConverter.ToString(arr));
// Convert bytes starting at different positions
for (int i = 0; i <= arr.Length - 4; i += 4) {
int result = BitConverter.ToInt32(arr, i);
Console.WriteLine("Starting at index {0}: {1}", i, result);
}
}
}
The output of the above code is −
Byte Array = 00-00-0A-0F-14-1A-20-22-28 Starting at index 0: 251658240 Starting at index 4: 571081236
Example with Known Integer Conversion
This example demonstrates converting a known integer value back and forth −
using System;
public class Demo {
public static void Main() {
int originalValue = 123456789;
Console.WriteLine("Original integer: {0}", originalValue);
// Convert int to byte array
byte[] bytes = BitConverter.GetBytes(originalValue);
Console.WriteLine("As bytes: {0}", BitConverter.ToString(bytes));
// Convert back to int
int convertedBack = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("Converted back to int: {0}", convertedBack);
Console.WriteLine("Values match: {0}", originalValue == convertedBack);
}
}
The output of the above code is −
Original integer: 123456789 As bytes: 15-CD-5B-07 Converted back to int: 123456789 Values match: True
Common Use Cases
- Network Programming − Converting received byte streams to integer values
- File I/O − Reading binary files that contain integer data
- Serialization − Converting between binary data and integer representations
- Protocol Implementation − Parsing binary protocols that encode integers as byte sequences
Conclusion
The BitConverter.ToInt32() method is essential for converting four consecutive bytes from a byte array into a 32-bit signed integer. It handles endianness automatically and is commonly used in binary data processing, network communication, and file operations where integer data is stored in byte format.
