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

เมธอด Convert.ToChar ใน C#

เมธอด Convert.ToChar ใช้เพื่อแปลงค่าที่ระบุเป็นจำนวนเต็ม Unicode

เราได้ประกาศตัวแปร sbyte:

sbyte byteVal = 200;

ตอนนี้ ให้ใช้เมธอด Convert.ToChar() เพื่อแปลงค่า sbyte เป็นจำนวนเต็ม Unicode:

charVal = Convert.ToChar(b);

มาดูตัวอย่างอีกตัวหนึ่ง

ตัวอย่าง

using System;

public class Demo
{
    public static void Main()
    {
        sbyte[] byteVal = { 92, 111, 115 };
        char charVal;
        foreach (sbyte b in byteVal)
        {
            charVal = Convert.ToChar(b);
            Console.WriteLine("{0} converted to '{1}'", b, charVal);
        }
    }
}

ผลลัพธ์

92 converted to '\'
111 converted to 'o'
115 converted to 's'