国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桦川县| 浙江省| 象山县| 民权县| 闽侯县| 溧水县| 温宿县| 岐山县| 临沭县| 渝中区| 门源| 固阳县| 郁南县| 雅安市| 安顺市| 会泽县| 米林县| 余江县| 日照市| 郧西县| 杭州市| 广安市| 安达市| 长岛县| 玉树县| 郁南县| 个旧市| 正安县| 达孜县| 重庆市| 台北市| 乾安县| 马龙县| 泌阳县| 平南县| 曲沃县| 华安县| 米易县| 塔城市| 额尔古纳市| 栾川县|