16进制在协议转换的时候用得还是比较多,下面几个例子供参考
十进制转为十六进制:
int a = 108;
String strA = a.ToString("x8");
十六进制转为十进制:
int b= Int32.Parse("266", System.Globalization.NumberStyles.HexNumber);
也可以自己封装如下类似的方法:
// 十六进制换算为十进制
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 - 1));
total += Convert.ToInt32(strNum) * Convert.ToInt32(power);
}
}
catch (System.Exception ex)
{
String strErorr = ex.ToString();
return 0;
}
return total;
}