diff --git a/num-converter/WebForm1.aspx.cs b/num-converter/WebForm1.aspx.cs index b11b88c..61db75d 100644 --- a/num-converter/WebForm1.aspx.cs +++ b/num-converter/WebForm1.aspx.cs @@ -11,14 +11,13 @@ namespace num_converter { protected void Page_Load(object sender, EventArgs e) { - } protected void btnConvert_Click(object sender, EventArgs e) { String Result = string.Empty; int Remainder; - int DecimalNum; + int decNum; int BinaryNum; int OctalNum; string hexVal = string.Empty; @@ -26,47 +25,43 @@ namespace num_converter //dec to binary if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 2) { - DecimalNum = Convert.ToInt32(txtInput.Text); - while (DecimalNum > 0) + decNum = Convert.ToInt32(txtInput.Text); + while (decNum > 0) { - Remainder = DecimalNum % 2; - DecimalNum /= 2; + Remainder = decNum % 2; + decNum /= 2; Result = Remainder.ToString() + Result; } + lblResult.Text = Result; } + //dec to oct if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 3) { - - DecimalNum = Convert.ToInt32(txtInput.Text); - while (DecimalNum > 0) + decNum = Convert.ToInt32(txtInput.Text, 8); + OctalNum = decNum; + Result = Convert.ToString(OctalNum); + /*while (decNum > 0) { - Remainder = DecimalNum % 8; + Remainder = decNum % 8; Result = Remainder.ToString() + Result; - DecimalNum /= 8; - } - - lblResult.Text = Result; + decNum /= 8; + */ + } + lblResult.Text = Result; } + //dec to hex if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 4) { - DecimalNum = Convert.ToInt32(txtInput.Text); - hexVal = DecimalNum.ToString("X"); - /*while (DecimalNum > 0) - { - Remainder = DecimalNum % 16; - string hexDigit = (Remainder < 10) - ? Remainder.ToString() - : ((char)('A' + Remainder - 10)).ToString(); - Result = hexDigit + Result; - DecimalNum /= 16; - }*/ + decNum = Convert.ToInt32(txtInput.Text); + hexVal = decNum.ToString("X"); Result = hexVal; lblResult.Text = Result; } + //bin to dec if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 1) { @@ -75,45 +70,29 @@ namespace num_converter lblResult.Text = Result; } + //binary to dec to oct if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 3) { BinaryNum = Convert.ToInt32(txtInput.Text, 2); - DecimalNum = BinaryNum; - while (DecimalNum > 0) + result = Convert.ToString(BinaryNum, 8) + /*decNum = BinaryNum; + while (decNum > 0) { - Remainder = DecimalNum % 2; - DecimalNum /= 2; + Remainder = decNum % 2; + decNum /= 2; Result = Remainder.ToString() + Result; - } + */} lblResult.Text = Result; } + //bin to hex if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 4) { - string binaryNum = Convert.ToInt32(txtInput.Text, 2).ToString("X"); - Result = binaryNum; - lblResult.Text = Result; - - /*int binRemain = binaryNum.Length % 4; - if (binRemain != 0) - { - binaryNum = new string('O', 4 - binRemain) + binaryNum; - } - - for (int i = 0; i < binaryNum.Length; i += 4) - { - string binaryGroup = binaryNum.Substring(i, 4); - DecimalNum = Convert.ToInt32(binaryGroup, 2); - string hexDigit = DecimalNum.ToString("X"); - Result += hexDigit; - } - - lblResult.Text = Result; - /* Result = BinaryNum.ToString(); - lblResult.Text = "0x" + Result;*/ - + string binaryNum = Convert.ToInt32(txtInput.Text, 2).ToString("X"); + Result = binaryNum; + lblResult.Text = Result; } //octal to decimal @@ -126,21 +105,22 @@ namespace num_converter if (ddlSourcePosNum.SelectedIndex == 3 && ddlTargetPosNum.SelectedIndex == 2) { - DecimalNum = Convert.ToInt32(txtInput.Text, 8); - - while (DecimalNum > 0) + OctalNum = Convert.ToInt32(txtInput.Text, 8); + Result = Convert.ToString(OctalNum, 2); + /*while (decNum > 0) { - Remainder = DecimalNum % 2; - DecimalNum /= 2; + Remainder = decNum % 2; + decNum /= 2; Result = Remainder.ToString() + Result; - } + */} + lblResult.Text = Result; } if (ddlSourcePosNum.SelectedIndex == 3 && ddlTargetPosNum.SelectedIndex == 4) { - DecimalNum = Convert.ToInt32(txtInput.Text, 8); - hexVal = DecimalNum.ToString("X"); + decNum = Convert.ToInt32(txtInput.Text, 8); + hexVal = decNum.ToString("X"); Result = hexVal; lblResult.Text = Result; } @@ -148,28 +128,26 @@ namespace num_converter if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 1) { hexVal = txtInput.Text; - DecimalNum = Convert.ToInt32(hexVal, 16); - Result = Convert.ToString(DecimalNum); + decNum = Convert.ToInt32(hexVal, 16); + Result = Convert.ToString(decNum); lblResult.Text = Result; } if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 2) { hexVal = txtInput.Text; - DecimalNum = Convert.ToInt32(hexVal, 16); - Result = Convert.ToString(DecimalNum, 2); + decNum = Convert.ToInt32(hexVal, 16); + Result = Convert.ToString(decNum, 2); lblResult.Text = Result; } if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 3) { hexVal = txtInput.Text; - DecimalNum = Convert.ToInt32(hexVal, 16); - Result = Convert.ToString(DecimalNum, 8); + decNum = Convert.ToInt32(hexVal, 16); + Result = Convert.ToString(decNum, 8); lblResult.Text = Result; } - - } protected void btnClear_Click(object sender, EventArgs e) @@ -178,7 +156,6 @@ namespace num_converter ddlTargetPosNum.SelectedIndex = 0; txtInput.Text = ""; lblResult.Text = ""; - } } } \ No newline at end of file