วิธี Convert.ToByte ใช้เพื่อแปลงค่าที่ระบุเป็นจำนวนเต็ม 8 บิตที่ไม่ได้ลงนาม
สมมติว่าเรามีตัวแปรถ่าน
Char charVal = ‘a’;
ตอนนี้ แปลงเป็นจำนวนเต็ม 8 บิตที่ไม่มีเครื่องหมาย
byte byteVal = Convert.ToByte(charVal);
เรามาดูตัวอย่างอื่นกันดีกว่า
ตัวอย่าง
using System;
public class Demo {
public static void Main() {
char[] charVal = { 'p', 'q', 'r', 's' };
foreach (char c in charVal) {
byte byteVal = Convert.ToByte(c);
Console.WriteLine("{0} is converted to = {1}", c, byteVal);
}
}
} ผลลัพธ์
p is converted to = 112 q is converted to = 113 r is converted to = 114 s is converted to = 115