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

C# BitConverter.ToUInt16() मेथड

C# में BitConverter.ToUInt16() मेथड बाइट ऐरे में निर्दिष्ट स्थिति पर दो बाइट्स से परिवर्तित 16-बिट अनसाइन्ड इंटीजर लौटाता है।

सिंटैक्स

public static ushort ToUInt16 (byte[] val, int begnIndex);

उपरोक्त, val बाइट ऐरे है, जबकि begnIndex val के भीतर शुरुआती स्थिति है।

उदाहरण 1

using System;

public class Demo {
    public static void Main() {
        byte[] arr = { 10, 20, 30, 40, 50 };
        int count = arr.Length;
        Console.Write("Byte Array... ");
        for (int i = 0; i < count; i++) {
            Console.Write("\n" + arr[i]);
        }
        Console.WriteLine("\n\nByte Array (String representation) = {0} ",
            BitConverter.ToString(arr));
        for (int i = 1; i < arr.Length - 1; i = i + 2) {
            ushort res = BitConverter.ToUInt16(arr, i);
            Console.WriteLine("\nValue = " + arr[i]);
            Console.WriteLine("Result = " + res);
        }
    }
}

आउटपुट

Byte Array...
10
20
30
40
50
Byte Array (String representation) = 0A-14-1E-28-32
Value = 20
Result = 7700
Value = 40
Result = 12840

उदाहरण 2

using System;

public class Demo {
    public static void Main() {
        byte[] arr = { 0, 0, 1, 3, 5, 7, 10, 16, 20, 34, 42, 55, 66, 75 };
        int count = arr.Length;
        Console.Write("Byte Array... ");
        for (int i = 0; i < count; i++) {
            Console.Write("\n" + arr[i]);
        }
        Console.WriteLine("\n\nByte Array (String representation) = {0} ",
            BitConverter.ToString(arr));
        for (int i = 1; i < arr.Length - 1; i = i + 2) {
            ushort res = BitConverter.ToUInt16(arr, i);
            Console.WriteLine("\nValue = " + arr[i]);
            Console.WriteLine("Result = " + res);
        }
    }
}

आउटपुट

Byte Array...
0
0
1
3
5
7
10
16
20
34
42
55
66
75
Byte Array (String representation) = 00-00-01-03-05-07-0A-10-14-22-2A-37-42-4B
Value = 0
Result = 256
Value = 3
Result = 1283
Value = 7
Result = 2567
Value = 16
Result = 5136
Value = 34
Result = 10786
Value = 55
Result = 16951
  1. - C# में Skip() मेथड का उपयोग

    C# में Skip() मेथड का उपयोग करके किसी सरणी (array) में निर्दिष्ट संख्या में तत्वों को छोड़ा जा सकता है। यह LINQ का एक एक्सटेंशन मेथड है जो System.Linq नेमस्पेस में पाया जाता है। उदाहरण मान लीजिए हमारे पास निम्न सरणी है: int[] arr = { 10, 20, 30, 40, 50 }; पहले दो तत्वों को छोड़ने के लिए Skip(2) क

  1. - C# में Array.BinarySearch विधि

    BinarySearch विधि का उपयोग करके सरणी तत्वों का स्थान प्राप्त करें। एक स्ट्रिंग सरणी सेट करें: string[] str = { a, m, i, t }; अब Array.BinarySearch का उपयोग करके वर्ण t का स्थान प्राप्त करें: Array.BinarySearch(str, t); यहां पूरा कोड है: उदाहरण using System; using System.Text; public class Demo {

  1. - C# में Clone() मेथड

    C# में Clone() मेथड का उपयोग ऐरे की एक समान कॉपी बनाने के लिए किया जाता है।आइए Clone() मेथड का उपयोग करके ऐरे को क्लोन करने का एक उदाहरण देखें:उदाहरणusing System; class Program {    static void Main() {       string[] arr = { "one", "two", "three&quo