用C#实现的十进制和十六进制的转换函数
2010-09-07 14:11
211 查看
1.十进制转十六进制:
public static string Ten2Hex(string ten)
{
ulong tenValue = Convert.ToUInt64(ten);
ulong divValue,resValue;
string hex = "";
do
{
divValue = (ulong)Math.Floor(tenValue/16);
resValue = tenValue%16;
hex = tenValue2Char(resValue) + hex;
tenValue = divValue;
}
while (tenValue >= 16);
if (tenValue != 0)
hex = tenValue2Char(tenValue) + hex;
return hex;
}
public static string tenValue2Char(ulong ten)
{
switch(ten)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return ten.ToString();
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
return "";
}
}
2.十六进制转十进制:
public static string Hex2Ten(string hex)
{
int ten = 0;
for (int i=0,j=hex.Length-1 ; i<hex.Length ; i++)
{
ten += HexChar2Value(hex.Substring(i,1)) * ((int)Math.Pow(16,j));
j--;
}
return ten.ToString();
}
public static int HexChar2Value(string hexChar)
{
switch (hexChar)
{
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
return Convert.ToInt32(hexChar);
case "a":
case "A":
return 10;
case "b":
case "B":
return 11;
case "c":
case "C":
return 12;
case "d":
case "D":
return 13;
case "e":
case "E":
return 14;
case "f":
case "F":
return 15;
default:
return 0;
}
}
public static string Ten2Hex(string ten)
{
ulong tenValue = Convert.ToUInt64(ten);
ulong divValue,resValue;
string hex = "";
do
{
divValue = (ulong)Math.Floor(tenValue/16);
resValue = tenValue%16;
hex = tenValue2Char(resValue) + hex;
tenValue = divValue;
}
while (tenValue >= 16);
if (tenValue != 0)
hex = tenValue2Char(tenValue) + hex;
return hex;
}
public static string tenValue2Char(ulong ten)
{
switch(ten)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return ten.ToString();
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
return "";
}
}
2.十六进制转十进制:
public static string Hex2Ten(string hex)
{
int ten = 0;
for (int i=0,j=hex.Length-1 ; i<hex.Length ; i++)
{
ten += HexChar2Value(hex.Substring(i,1)) * ((int)Math.Pow(16,j));
j--;
}
return ten.ToString();
}
public static int HexChar2Value(string hexChar)
{
switch (hexChar)
{
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
return Convert.ToInt32(hexChar);
case "a":
case "A":
return 10;
case "b":
case "B":
return 11;
case "c":
case "C":
return 12;
case "d":
case "D":
return 13;
case "e":
case "E":
return 14;
case "f":
case "F":
return 15;
default:
return 0;
}
}
相关文章推荐
- C#中如何实现二进制、十进制、十六进制的互转
- C#中二进制、十进制和十六进制互相转换的方法
- 用递归方式实现十进制转十六进制
- Oracle中二进制、八进制、十进制和十六进制相互转换函数
- 整理C# 二进制,十进制,十六进制 互转
- Sql server实现十六进制和十进制的转换
- Oracle中的二进制、八进制、十进制、十六进制相互转换函数
- C#控制台基础 tostring(x2),十进制转换成十六进制格式对齐的问题
- 整理C# 二进制,十进制,十六进制 互转
- 整理C# 二进制,十进制,十六进制 互转
- C# 进制转换(二进制、八进制、十进制、十六进制互转)
- 整理C# 二进制,十进制,十六进制 互转
- 整理C# 二进制,十进制,十六进制 互转附加(BCC异或校验法)
- C# 进制转换(二进制、十六进制、十进制互转)
- 十进制转化为二,八,十六进制用栈实现(c语言编)
- C#中二进制、十进制和十六进制互相转换的方法
- 十进制和十六进制之间的转换函数总结
- 使用无符号右移-&-栈 实现 十进制转十六进制
- [置顶] 栈应用:实现二进制转八进制、十进制、十六进制
- C#中二进制、八进制、十六进制和十进制之间的相互转化问题