การแปลงจำนวนเต็มเป็นเลขฐานสิบหก
จำนวนเต็มสามารถแปลงเป็นเลขฐานสิบหกได้โดยใช้วิธีการขยาย string.ToString()
Integer Value: 500 Hexadecimal Value: 1F4
การแปลงเลขฐานสิบหกเป็นจำนวนเต็ม −
ค่าเลขฐานสิบหกสามารถแปลงเป็นจำนวนเต็มได้โดยใช้ int.Parse หรือ convert.ToInt32
int.Parse − แปลงการแสดงสตริงของตัวเลขเป็นจำนวนเต็มที่มีลายเซ็นแบบ 32 บิต ค่าที่ส่งกลับบ่งชี้ว่าการดำเนินการสำเร็จหรือไม่
Hexadecimal Value: 1F4 Integer Value: 500
Convert.ToInt32 −แปลงค่าที่ระบุเป็นจำนวนเต็มที่ลงนามแบบ 32 บิต
Hexadecimal Value: 1F4 Integer Value: 500
การแปลงจำนวนเต็มเป็นเลขฐานสิบหก −
สตริง hexValue =integerValue.ToString("X");
ตัวอย่าง
using System;
namespace DemoApplication{
public class Program{
public static void Main(){
int integerValue = 500;
Console.WriteLine($"Integer Value: {integerValue}");
string hexValue = integerValue.ToString("X");
Console.WriteLine($"Hexadecimal Value: {hexValue}");
Console.ReadLine();
}
}
} ผลลัพธ์
ผลลัพธ์ของโค้ดด้านบนคือ
Integer Value: 500 Hexadecimal Value: 1F4
การแปลงเลขฐานสิบหกเป็นจำนวนเต็ม −
ตัวอย่างการใช้ int.Parse −
ตัวอย่าง
using System;
namespace DemoApplication{
public class Program{
public static void Main(){
string hexValue = "1F4";
Console.WriteLine($"Hexadecimal Value: {hexValue}");
int integerValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine($"Integer Value: {integerValue}");
Console.ReadLine();
}
}
} ผลลัพธ์
ผลลัพธ์ของโค้ดด้านบนคือ
Hexadecimal Value: 1F4 Integer Value: 500
ตัวอย่างการใช้ Convert.ToInt32 −
ตัวอย่าง
using System;
namespace DemoApplication{
public class Program{
public static void Main(){
string hexValue = "1F4";
Console.WriteLine($"Hexadecimal Value: {hexValue}");
int integerValue = Convert.ToInt32(hexValue, 16);
Console.WriteLine($"Integer Value: {integerValue}");
Console.ReadLine();
}
}
} ผลลัพธ์
ผลลัพธ์ของโค้ดด้านบนคือ
Hexadecimal Value: 1F4 Integer Value: 500