转至:http://www.cnblogs.com/fwind/archive/2012/04/13/2445380.html
在C#中,十进制和十六进制转换非常简单,方法如下:
十进制转为十六进制:
int a = ;
String strA = a.ToString("x8");
十六进制转为十进制:
int b= Int32.Parse("", System.Globalization.NumberStyles.HexNumber);
也可以使用下述方法:
/// <summary>
/// 十六进制换算为十进制
/// </summary>
/// <param name="strColorValue"></param>
/// <returns></returns>
public static int GetHexadecimalValue(String strColorValue)
{
char[] nums = strColorValue.ToCharArray();
int total = ;
try
{
for (int i = ; i < nums.Length; i++)
{
String strNum = nums[i].ToString().ToUpper();
switch (strNum)
{
case "A":
strNum = "";
break;
case "B":
strNum = "";
break;
case "C":
strNum = "";
break;
case "D":
strNum = "";
break;
case "E":
strNum = "";
break;
case "F":
strNum = "";
break;
default:
break;
}
double power = Math.Pow(, Convert.ToDouble(nums.Length - i - ));
total += Convert.ToInt32(strNum) * Convert.ToInt32(power);
} }
catch (System.Exception ex)
{
String strErorr = ex.ToString();
return ;
} return total;
}