C # Implementation of the algorithm of binary decimal number in binary and 86 or 16

Source: Internet
Author: User

Binary swap to Decimal method, for example:

Binary number: 11001

Decimal number: 1*24+1*23+0*22+0*21+1*20=25

Octal to decimal method, for example:

Octal Number: 32007

Decimal number: 3*84+2*83+0*82+0*81+7*80=13319

Hexadecimal swap method for decimal, for example:

Hexadecimal number: a20f (because the hexadecimal number is represented by a~f between 10~15, so the calculation will a~f with 10~15)

Decimal number: 10*83+2*82+0*81+15*80=41487

Through the above two or eight, hexadecimal decimal number of the calculation method, you can calculate the code is:

//Parameter Num is the number that needs to be converted, n is the binary of the number 
public string ToD (string Num, int n)
{
char[] Nums=num.tochararray ();
int d = 0;
for (int i = 0; I <nums. Length; i++)
{
String number=nums [i]. ToString ();
if (n = =)
{
Switch (number. ToUpper ())
{
Case "A":
Number = "Ten";
Break
Case "B":
Number = "one";
Break
Case "C":
Number = "a";
Break
Case "D":
Number = ";"
Break
Case "E":
Number = "number";
Break
Case "F":
Number = ";"
Break
}
}
Double power = Math.pow (Convert.todouble (n), COnvert. ToDouble (nums. Length-(i + 1));
D = d + convert.toint32 (number) * CONVERT.TOINT32 (Power);
}
return d.tostring ();
}

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.