在C#中,十進位制和十六進位制轉換非常簡單,方法如下:
十進位制轉為十六進位制:
1 int a = 100;
2 String strA = a.ToString("x8");
十六進位制轉為十進位制:
1 int b= Int32.Parse("266", System.Globalization.NumberStyles.HexNumber);
也可以使用下述方法:
/// <summary>
/// 十六進位制換算為十進位制
/// </summary>
/// <param name="strColorValue"></param>
/// <returns></returns>
public static int GetHexadecimalValue(String strColorValue)
{
char[] nums = strColorValue.ToCharArray();
int total = 0;
try
for (int i = 0; i < nums.Length; i++)
String strNum = nums[i].ToString().ToUpper();
switch (strNum)
case "A":
strNum = "10";
break;
case "B":
strNum = "11";
case "C":
strNum = "12";
case "D":
strNum = "13";
case "E":
strNum = "14";
case "F":
strNum = "15";
default:
}
double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - ));
total += Convert.ToInt32(strNum) * Convert.ToInt32(power);
catch (System.Exception ex)
String strErorr = ex.ToString();
return 0;
return total;
在C#中,十進位制和十六進位制轉換非常簡單,方法如下:
十進位制轉為十六進位制:
1 int a = 100;
2 String strA = a.ToString("x8");
十六進位制轉為十進位制:
1 int b= Int32.Parse("266", System.Globalization.NumberStyles.HexNumber);
也可以使用下述方法:
/// <summary>
/// 十六進位制換算為十進位制
/// </summary>
/// <param name="strColorValue"></param>
/// <returns></returns>
public static int GetHexadecimalValue(String strColorValue)
{
char[] nums = strColorValue.ToCharArray();
int total = 0;
try
{
for (int i = 0; i < nums.Length; i++)
{
String strNum = nums[i].ToString().ToUpper();
switch (strNum)
{
case "A":
strNum = "10";
break;
case "B":
strNum = "11";
break;
case "C":
strNum = "12";
break;
case "D":
strNum = "13";
break;
case "E":
strNum = "14";
break;
case "F":
strNum = "15";
break;
default:
break;
}
double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - ));
total += Convert.ToInt32(strNum) * Convert.ToInt32(power);
}
}
catch (System.Exception ex)
{
String strErorr = ex.ToString();
return 0;
}
return total;
}