Common algorithms (C #): Convert binary, octal, and hexadecimal to decimal.

Source: Internet
Author: User
Tags binary to decimal

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace extodd
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}


// The num parameter is the number to be converted, and N is the base number of The number.
Public String Todd (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 = 16)
{
Switch (number. toupper ())
{
Case "":
Number = "10 ";
Break;
Case "B ":
Number = "11 ";
Break;
Case "C ":
Number = "12 ";
Break;
Case "D ":
Number = "13 ";
Break;
Case "E ":
Number = "14 ";
Break;
Case "F ":
Number = "15 ";
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 ();
}

Private void btnbtod_click (Object sender, eventargs E)
{
This.txt btodd. Text = tod(this.txt B. Text, 2); // convert binary to decimal
}
Private void btnotod_click (Object sender, eventargs E)
{
This.txt otodd. Text = tod(this.txt O. Text, 8); // convert octal to decimal
}

Private void btnxtod_click (Object sender, eventargs E)
{
This.txt xtodd. Text = tod(this.txt X. Text, 16); // convert the hexadecimal format to decimal
}

// Only 0, 1, and backspace keys are allowed.
Private void txtb_keypress (Object sender, keypresseventargs E)
{
If (E. keychar! = 48 & E. keychar! = 49 & E. keychar! = 8)
{
E. Handled = true;
}
}
// Only 0 ~ 8. Return key
Private void txto_keypress (Object sender, keypresseventargs E)
{
If (! (E. keychar> = 48 & E. keychar <= 55) & E. keychar! = 8)
{
E. Handled = true;
}
}
// Only 0 ~ 9. ~ F and return key
Private void txtx_keypress (Object sender, keypresseventargs E)
{

If (! (E. keychar> = 48 & E. keychar <= 57 )&&! (E. keychar> = 97 & E. keychar <= 102) & E. keychar! = 8)
{
E. Handled = true;
}
}


}
}

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.