Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด BitConverter.ToBoolean() ใน C#

เมธอด BitConverter.ToBoolean() ใน C# ใช้สำหรับคืนค่า Boolean ที่แปลงจากไบต์ตำแหน่งที่ระบุในอาร์เรย์ไบต์

ไวยากรณ์

ไวยากรณ์มีดังนี้:

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

พารามิเตอร์:

  • arr: อาร์เรย์ไบต์
  • startIndex: ดัชนีของไบต์ภายในค่า

ตัวอย่าง

ตัวอย่างการใช้งานเมธอด BitConverter.ToBoolean():

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); 
        } 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้:

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

คำอธิบาย: ค่าไบต์ที่ไม่ใช่ 0 จะแปลงเป็น true, ค่า 0 จะแปลงเป็น false