C#中如何将整型数据转换成十六进制
展开全部
#region 将一个整数转换为16进制输出,(不得使用系统函数)
static void Test()
{
Console.Write("请输入一个整数如(1000):");
int value = int.Parse(Console.ReadLine());
string s = "";
getValue64(value, ref s); //计算16进制的逆序值
string s2 = ""; //记录正确的16进制值
foreach (char c in s)
{
s2 = c + s2;
}
Console.WriteLine("{0}转换为16进制为{1}", value, s2);
}
//递归计算值
static void getValue(int value, ref string s)
{
if (value / 16 < 1)
{
s += snap64(Convert.ToString(value));
}
else
{
s += snap64(Convert.ToString(value % 16));
getValue64(value / 16, ref s);
}
}
static string snap(string s)
{
switch (s)
{
case "10":
s = "A";
break;
case "11":
s = "B";
break;
case "12":
s = "C";
break;
case "13":
s = "D";
break;
case "14":
s = "E";
break;
case "15":
s = "F";
break;
default:
break;
}
return s;
}
#endregion
static void Test()
{
Console.Write("请输入一个整数如(1000):");
int value = int.Parse(Console.ReadLine());
string s = "";
getValue64(value, ref s); //计算16进制的逆序值
string s2 = ""; //记录正确的16进制值
foreach (char c in s)
{
s2 = c + s2;
}
Console.WriteLine("{0}转换为16进制为{1}", value, s2);
}
//递归计算值
static void getValue(int value, ref string s)
{
if (value / 16 < 1)
{
s += snap64(Convert.ToString(value));
}
else
{
s += snap64(Convert.ToString(value % 16));
getValue64(value / 16, ref s);
}
}
static string snap(string s)
{
switch (s)
{
case "10":
s = "A";
break;
case "11":
s = "B";
break;
case "12":
s = "C";
break;
case "13":
s = "D";
break;
case "14":
s = "E";
break;
case "15":
s = "F";
break;
default:
break;
}
return s;
}
#endregion
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询