เมธอด Convert.ToUInt16() แปลงค่าที่ระบุเป็นจำนวนเต็มไม่มีเครื่องหมาย 16 บิต (ushort หรือ UInt16) ช่วงค่า 0 ถึง 65,535
ตัวอย่างแปลงจาก String
string str = "1307"; ushort res = Convert.ToUInt16(str); // 1307
ตัวอย่างโค้ดเต็ม
using System;
public class Demo
{
public static void Main()
{
string str = "1307";
ushort res = Convert.ToUInt16(str);
Console.WriteLine("Converted string '{0}' to {1}", str, res);
}
}
ผลลัพธ์
Converted string '1307' to 1307
ข้อยกเว้น: OverflowException หากค่าติดลบหรือเกิน 65,535, FormatException หากรูปแบบไม่ถูกต้อง