将整数转换为十六进制
可以使用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