[转载]c#中int 转string 16进制和16转double的方法
0x开头的16进制没有负数和小数
#region change hex to double
private Double HexConverToDouble(string hexString)
{
if (hexString == "")
{
return 0;
}
string data;
if (hexString.StartsWith("0x"))
{
data = hexString.Substring(2);
}
else
{
data = hexString;
}
char[] eachData = data.ToCharArray();
Double result = 0;
for (int i = 0; i < eachData.Length; i++)
{
char charValue = eachData[i];//eachData[m];
Double x = 16;//如果是八进制则写成8就可以
Double y = System.Convert.ToDouble(eachData.Length - i - 1);
switch (charValue)
{
case ''0'':
break;
case ''1'':
result += 1 * Math.Pow(x,y);
break;
case ''2'':
result += 2 * Math.Pow(x,y);
break;
case ''3'':
result += 3 * Math.Pow(x,y);
break;
case ''4'':
result += 4 * Math.Pow(x,y);
break;
case ''5'':
result += 5 * Math.Pow(x,y);
break;
case ''6'':
result += 6 * Math.Pow(x,y);
break;
case ''7'':
result += 7 * Math.Pow(x,y);
break;
case ''8'':
result += 8 * Math.Pow(x,y);
break;
case ''9'':
result += 9 * Math.Pow(x,y);
break;
case ''A'':
result += 10 * Math.Pow(x,y);
break;
case ''B'':
result += 11 * Math.Pow(x,y);
break;
case ''C'':
result += 12 * Math.Pow(x,y);
break;
case ''D'':
result += 13 * Math.Pow(x,y);
break;
case ''E'':
result += 14 * Math.Pow(x,y);
break;
case ''F'':
result += 15 * Math.Pow(x,y);
break;
case ''a'':
result += 10 * Math.Pow(x,y);
break;
case ''b'':
result += 11 * Math.Pow(x,y);
break;
case ''c'':
result += 12 * Math.Pow(x,y);
break;
case ''d'':
result += 13 * Math.Pow(x,y);
break;
case ''e'':
result += 14 * Math.Pow(x,y);
break;
case ''f'':
result += 15 * Math.Pow(x,y);
break;
default :
break;
}
}
return result;
}
#region convert the int32 to hex(string) //这个方法通用性不好,只能是int的转string的16进制
private string specInttoString(int source)//被主要方法调用的一个辅助方法
{
if(source <10)
{
return source.ToString();
}
else
{
switch(source)
{
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 "";
}
}
}
private string INTtoHEx(int source)//主要方法
{
if(source <10)
{
return "0x" + source.ToString();
}
else if (source <=15)
{
return "0x" + specInttoString(source);
}
else
{
int raiseNum = 16;
int addNum = 16;
int positionNum = 1;
while((source - addNum) >= 0)
{
positionNum++;
addNum = addNum * raiseNum;
}
int[] valuePositionNum = new int[positionNum];
for(int i = 0;i
{
valuePositionNum[i] = 0;
}
int[] valueAddNum = new int[positionNum];
for(int i = 0;i
{
valueAddNum[i] = Convert.ToInt32( Math.Pow(raiseNum,i));
}
int[] decreaseSource = new int[positionNum];
decreaseSource[positionNum -1] = source;
for(int i = positionNum -1;i>=0;i--)
{
while((decreaseSource[i] - valueAddNum[i] ) >=0)
{
if(i != 0)
decreaseSource[i -1] = decreaseSource[i] - valueAddNum[i] ;
valuePositionNum[i]++;
valueAddNum[i]= valueAddNum[i] +Convert.ToInt32( Math.Pow(raiseNum,i));
}
}
string[] stringValuePositionNum = new string[positionNum];
for(int i = 0;i
{
stringValuePositionNum[i] = specInttoString(valuePositionNum[i]);
}
string result = "0x";
for(int i = positionNum -1;i>=0;i--)
{
result = result + stringValuePositionNum[i];
}
return result;
// string[] hexList = new string[positionNum + 1];
// hexList[positionNum] = specInttoString(positionNum);
}
}
#endregion
#endregion
[此贴子已经被作者于2006-3-12 13:37:43编辑过]