在C#中,十进制和十六进制转换非常简单,方法如下:
十进制转为十六进制:
1 int a = 100; 2 String strA = a.ToString("x8");
十六进制转为十进制:
1 int b= Int32.Parse("266", System.Globalization.NumberStyles.HexNumber);
也可以使用下述方法:
1 /// <summary> 2 /// 十六进制换算为十进制 3 /// </summary> 4 /// <param name="strColorValue"></param> 5 /// <returns></returns> 6 public static int GetHexadecimalValue(String strColorValue) 7 { 8 char[] nums = strColorValue.ToCharArray(); 9 int total = 0; 10 try 11 { 12 for (int i = 0; i < nums.Length; i++) 13 { 14 String strNum = nums[i].ToString().ToUpper(); 15 switch (strNum) 16 { 17 case "A": 18 strNum = "10"; 19 break; 20 case "B": 21 strNum = "11"; 22 break; 23 case "C": 24 strNum = "12"; 25 break; 26 case "D": 27 strNum = "13"; 28 break; 29 case "E": 30 strNum = "14"; 31 break; 32 case "F": 33 strNum = "15"; 34 break; 35 default: 36 break; 37 } 38 double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - 1)); 39 total += Convert.ToInt32(strNum) * Convert.ToInt32(power); 40 } 41 42 } 43 catch (System.Exception ex) 44 { 45 String strErorr = ex.ToString(); 46 return 0; 47 } 48 49 50 return total; 51 }