Chuyển Integer sang Hexadecimal
Integer chuyển sang hexadecimal bằng phương thức string.ToString() extension.
Integer Value: 500 → Hexadecimal Value: 1F4
Chuyển Hexadecimal sang Integer
Hexadecimal chuyển sang integer dùng int.Parse hoặc Convert.ToInt32.
- int.Parse - Chuyển string representation của số thành 32-bit signed integer. Trả về giá trị chỉ thành công hay không.
- Convert.ToInt32 - Chuyển giá trị chỉ định thành 32-bit signed integer.
Hexadecimal Value: 1F4 → Integer Value: 500
Chuyển Integer sang Hexadecimal
string hexValue = integerValue.ToString("X");
Ví dụ
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();
}
}
}
Kết quả
Integer Value: 500 Hexadecimal Value: 1F4
Chuyển Hexadecimal sang Integer
Ví dụ dùng 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();
}
}
}
Kết quả
Hexadecimal Value: 1F4 Integer Value: 500
Ví dụ dùng 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();
}
}
}
Kết quả
Hexadecimal Value: 1F4 Integer Value: 500