欢迎光临
我们一直在努力

c#中int 转string 16进制和16转double的方法-.NET教程,C#语言

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#中int 转string 16进制和16转double的方法-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章