परिचय
C# में BitConverter.ToInt32() मेथड बाइट ऐरे में निर्दिष्ट स्थिति से शुरू होने वाले चार बाइट्स को 32-बिट साइन इंटीजर (int) में बदलता है।
सिंटैक्स
public static int ToInt32(byte[] value, int startIndex);
value बाइट ऐरे है, और startIndex value में शुरुआती स्थिति है।
उदाहरण 1: 5-तत्व बाइट ऐरे
using System;
public class Demo {
public static void Main() {
byte[] arr = { 10, 20, 30, 40, 50 };
Console.WriteLine("बाइट ऐरे = {0}", BitConverter.ToString(arr));
for (int i = 1; i < arr.Length - 3; i = i + 4) {
int res = BitConverter.ToInt32(arr, i);
Console.WriteLine("\nमान = " + arr[i]);
Console.WriteLine("परिणाम = " + res);
}
}
}
आउटपुट
बाइट ऐरे = 0A-14-1E-28-32 मान = 20 परिणाम = 841489940
उदाहरण 2: 9-तत्व बाइट ऐरे
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0, 0, 10, 15, 20, 26, 32, 34, 40 };
Console.WriteLine("बाइट ऐरे = {0}", BitConverter.ToString(arr));
for (int i = 1; i < arr.Length - 3; i = i + 4) {
int res = BitConverter.ToInt32(arr, i);
Console.WriteLine("\nमान = " + arr[i]);
Console.WriteLine("परिणाम = " + res);
}
}
}
आउटपुट
बाइट ऐरे = 00-00-0A-0F-14-1A-20-22-28 मान = 0 परिणाम = 336529920 मान = 26 परिणाम = 673325082
महत्वपूर्ण नोट्स
startIndexसेstartIndex + 3तक के 4 बाइट्स पढ़ता है।- एंडियननेस (Endianness) सिस्टम पर निर्भर करती है (आमतौर पर लिटिल-एंडियन)।
BitConverter.IsLittleEndianसे सिस्टम एंडियननेस जांचें।- क्रॉस-प्लेटफॉर्म सुसंगतता के लिए
BinaryPrimitives(.NET Core 2.1+) का उपयोग करें। - ऐरे बाउंड्स चेक किया जाता है - सीमा से बाहर
ArgumentExceptionफेंकता है।