কম্পিউটার টিউটোরিয়াল

C# BitConverter.ToString(Byte[]) মেথড

C#-এ BitConverter.ToString() মেথডটি বাইট অ্যারে-র প্রতিটি উপাদানকে হেক্সাডেসিমাল স্ট্রিং রিপ্রেজেন্টেশনে রূপান্তর করে, যেখানে বাইটগুলো হাইফন (-) দিয়ে আলাদা থাকে।

সিনট্যাক্স

public static string ToString (byte[] val);

এখানে val হলো বাইট অ্যারে।

উদাহরণ ১

using System;

public class Demo {
    public static void Main() {
        byte[] arr = {0, 10, 2, 5, 32, 45};
        int count = arr.Length;
        Console.Write("Byte Array... ");
        for (int i = 0; i < count; i++) {
            Console.Write("\n" + arr[i]);
        }
        Console.WriteLine("\nByte Array (String representation) = {0} ", BitConverter.ToString(arr));
    }
}

আউটপুট

Byte Array...
0
10
2
5
32
45
Byte Array (String representation) = 00-0A-02-05-20-2D

উদাহরণ ২

এখানে ToString() এর পাশাপাশি ToInt16() মেথডও ব্যবহার করা হয়েছে।

using System;

public class Demo {
    public static void Main() {
        byte[] arr = { 0, 0, 7, 10, 18, 20, 25, 26, 32};
        int count = arr.Length;
        Console.Write("Byte Array... ");
        for (int i = 0; i < count; i++) {
            Console.Write("\n" + arr[i]);
        }
        Console.WriteLine("\nByte Array (String representation) = " + BitConverter.ToString(arr));
        for (int i = 0; i < arr.Length - 1; i = i + 2) {
            short res = BitConverter.ToInt16(arr, i);
            Console.WriteLine("\nValue = " + arr[i]);
            Console.WriteLine("Result = " + res);
        }
    }
}

আউটপুট

Byte Array...
0
0
7
10
18
20
25
26
32
Byte Array (String representation) = 00-00-07-0A-12-14-19-1A-20
Value = 0
Result = 0
Value = 7
Result = 2567
Value = 18
Result = 5138
Value = 25
Result = 6681
  1. C#-এ অ্যারে ক্লাসের ToString() মেথড ব্যবহার

  2. C# Convert.ToString পদ্ধতি

  3. C#-এ Skip মেথড

  4. C#-এ Array.BinarySearch মেথড