c#中int 转string 16进制和16转double的方法
2024-07-21 02:19:35
供稿:网友
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