Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में BitConverter.ToInt32() मेथड - बाइट ऐरे से 32-बिट इंटीजर बदलें

परिचय

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 फेंकता है।
  1. - C# Convert.ToInt32 विधि

    टेक्स्ट को पूर्णांक में परिवर्तित करने के लिए Convert.ToInt32 विधि का उपयोग करें। उदाहरण पहले स्ट्रिंग सेट करें: string str = 12; अब उपरोक्त स्ट्रिंग को संख्या में बदलने के लिए Convert.ToInt32() विधि का उपयोग करें: Convert.ToInt32(str); पूर्ण कोड using System; class Program { static void Main()

  1. - C# में Convert.ToInt32 विधि

    निर्दिष्ट मान को 32-बिट हस्ताक्षरित पूर्णांक में परिवर्तित करने के लिए Convert.ToInt32() विधि का उपयोग करें। उदाहरण आइए एक डबल चर लें: double doubleNum = 11.53; अब Convert.ToInt32 विधि का उपयोग करके इसे Int32 में बदलें: int intNum; intNum = Convert.ToInt32(doubleNum); पूर्ण कोड using System; public

  1. - C# में SequenceEqual मेथड

    SequenceEqual मेथड का उपयोग संग्रहों की समानता के लिए परीक्षण करने के लिए किया जाता है।आइए तीन स्ट्रिंग सरणियाँ सेट करें:string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This",