Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức BitConverter.ToBoolean() trong C#

Phương thức BitConverter.ToBoolean() trong C# trả về giá trị Boolean được chuyển đổi từ byte tại vị trí được chỉ định trong mảng byte.

Cú pháp

Cú pháp như sau:

public static bool ToBoolean(byte[] arr, int startIndex);

Tham số arr là mảng byte, startIndex là chỉ số của byte trong giá trị.

Ví dụ - Chuyển đổi mảng byte thành Boolean

using System;

public class Demo
{
    public static void Main()
    {
        byte[] arr = { 50, 100 };
        Console.WriteLine("Array values...");
        for (int i = 0; i < arr.Length; i++)
        {
            Console.WriteLine("{0} ", arr[i]);
        }
        Console.WriteLine("\nConverted values...");
        for (int index = 0; index < arr.Length; index++)
        {
            bool res = BitConverter.ToBoolean(arr, index);
            Console.WriteLine("" + res);
        }
    }
}

Kết quả

Array values... 50 100
Converted values... True True