ใช้ Composite Formatting {0:X} หรือ {0:x} แสดงตัวเลขในฐานสิบหก X = พิมพ์ใหญ่, x = พิมพ์เล็ก สามารถระบุความกว้างได้ (เช่น X8)
ตัวอย่างโค้ด
using System;
class Demo
{
static void Main()
{
int a = 12250;
Console.WriteLine("{0:x}", a); // 2fda
Console.WriteLine("{0:x8}", a); // 00002fda
Console.WriteLine("{0:X}", a); // 2FDA
Console.WriteLine("{0:X8}", a); // 00002FDA
}
}
ผลลัพธ์
2fda 00002fda 2FDA 00002FDA
ใช้ได้กับ: byte, short, int, long, และ unsigned counterparts