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;
}
}
}
}