method of int to string 16 and 16 double in C #

Source: Internet
Author: User
Tags pow tostring
16 in the beginning of 0x there are no negative numbers or decimals

#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;//If it's octal, then write 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 = Ten * MATH.POW (x,y);

Break

Case ' B ':

result = one * MATH.POW (x,y);

Break

Case ' C ':

Result + = Math.pow (x,y);

Break

Case ' D ':

Result + * MATH.POW (x,y);

Break

Case ' E ':

Result + * MATH.POW (x,y);

Break

Case ' F ':

result = * MATH.POW (x,y);

Break

Case ' a ':

result = Ten * MATH.POW (x,y);

Break

Case ' B ':

result = one * MATH.POW (x,y);

Break

Case ' C ':

Result + = Math.pow (x,y);

Break

Case ' d ':

Result + * MATH.POW (x,y);

Break

Case ' E ':

Result + * MATH.POW (x,y);

Break

Case ' F ':

result = * MATH.POW (x,y);

Break

Default:

Break

}


}

return result;

}

#region Convert the Int32 to Hex (string)//This method is not universal, it can only be 16 of the int's turn string

private string specinttostring (int source)//an auxiliary method called by the primary method

{

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)//Main method

{

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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.