1.从十六进制转换为十进制
/// <summary>
/// 十六进制转换到十进制
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public static string Hex2Ten(string hex)
{
int ten = ;
for (int i = , j = hex.Length - ; i < hex.Length; i++)
{
ten += HexChar2Value(hex.Substring(i, )) * ((int)Math.Pow(, j));
j--;
}
return ten.ToString();
} public static int HexChar2Value(string hexChar)
{
switch (hexChar)
{
case "":
case "":
case "":
case "":
case "":
case "":
case "":
case "":
case "":
case "":
return Convert.ToInt32(hexChar);
case "a":
case "A":
return ;
case "b":
case "B":
return ;
case "c":
case "C":
return ;
case "d":
case "D":
return ;
case "e":
case "E":
return ;
case "f":
case "F":
return ;
default:
return ;
}
}
this.txtStartShi.Text = Hex2Ten(this.txtStartSN.Text.Trim().Substring(, ));
this.txtEndShi.Text = Hex2Ten(this.txtEndSN.Text.Trim().Substring(, ));
2.从十进制转换为十六进制
/// <summary>
/// 从十进制转换到十六进制
/// </summary>
/// <param name="ten"></param>
/// <returns></returns>
public static string Ten2Hex(string ten)
{
ulong tenValue = Convert.ToUInt64(ten);
ulong divValue, resValue;
string hex = "";
do
{
//divValue = (ulong)Math.Floor(tenValue / 16); divValue = (ulong)Math.Floor((decimal)(tenValue / )); resValue = tenValue % ;
hex = tenValue2Char(resValue) + hex;
tenValue = divValue;
}
while (tenValue >= );
if (tenValue != )
hex = tenValue2Char(tenValue) + hex;
return hex;
} public static string tenValue2Char(ulong ten)
{
switch (ten)
{
case :
case :
case :
case :
case :
case :
case :
case :
case :
case :
return ten.ToString();
case :
return "A";
case :
return "B";
case :
return "C";
case :
return "D";
case :
return "E";
case :
return "F";
default:
return "";
}
}
int StartSN = Convert.ToInt32(this.txtStartShi.Text.Trim());
int EndSN = Convert.ToInt32(this.txtEndShi.Text.Trim()); for (int i = ; i < EndSN - StartSN + ; i++)
{
listBox1.Items.Add("" + Ten2Hex((Convert.ToDouble(this.txtStartShi.Text) + i).ToString()));
}
/// <summary>
/// 10进制转34进制
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public static string Ten2ThirtyFour(int parameter)
{
string[] radix = { "", "", "", "", "", "", "", "", "", "", "A", "B", "C", "D",
"E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };
string result = "";
int len = ;
int remainder = ;
len = parameter / ;
remainder = parameter % ;
result = radix[remainder];
while (len > )
{
remainder = len % ;
len = len / ;
result = radix[remainder] + result;
}
return result;
}