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

เมธอด BitConverter.ToChar() ใน C# พร้อมตัวอย่าง


เมธอด BitConverter.ToChar() ใน C# ใช้สำหรับคืนค่า Unicode Character ที่แปลงจาก 2 ไบต์ ณ ตำแหน่งที่ระบุใน Byte Array

Syntax

public static char ToChar (byte[] value, int begnIndex);

value คือ Byte Array, begnIndex คือตำแหน่งเริ่มต้นใน value

ตัวอย่าง

using System; 
public class Demo { 
    public static void Main() { 
        byte[] arr = { 0, 20, 50, 65 }; 
        Console.WriteLine("Array = {0} ", BitConverter.ToString(arr)); 
        for (int i = 1; i < arr.Length - 1; i = i + 2) { 
            char values = BitConverter.ToChar(arr, i); 
            Console.WriteLine("Value = " + arr[i]); 
            Console.WriteLine("Result = " + values); 
        } 
    } 
}

ผลลัพธ์

Array = 00-14-32-41 
Value = 20 
Result = ㈔

ตัวอย่างเพิ่มเติม

using System; 
public class Demo { 
    public static void Main() { 
        byte[] arr = { 50, 13, 23, 18, 160 }; 
        Console.WriteLine("Array = {0} ", BitConverter.ToString(arr)); 
        for (int i = 1; i < arr.Length - 1; i = i + 2) { 
            char values = BitConverter.ToChar(arr, i); 
            Console.WriteLine("Value = " + arr[i]); 
            Console.WriteLine("Result = " + values); 
        } 
    } 
}

ผลลัพธ์

Array = 32-0D-17-12-A0 
Value = 13 
Result = ᜍ 
Value = 18 
Result = ꀒ