国产探花免费观看_亚洲丰满少妇自慰呻吟_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



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通州市| 泾川县| 敖汉旗| 乌审旗| 江都市| 新营市| 德昌县| 岢岚县| 枣阳市| 高碑店市| 嘉禾县| 长武县| 鄯善县| 新民市| 桦川县| 嘉定区| 金湖县| 柳林县| 峨边| 尼勒克县| 林芝县| 辉南县| 龙陵县| 泗水县| 高清| 丰都县| 扶余县| 柳河县| 鄂托克前旗| 平潭县| 沅陵县| 石台县| 卢氏县| 保靖县| 永清县| 茂名市| 盈江县| 宝坻区| 德惠市| 连平县| 弥渡县|