From f28dd6d5eae2eab00f934a00ef39eac7d3228c5e Mon Sep 17 00:00:00 2001 From: lLewdie Date: Fri, 22 Sep 2023 15:57:14 -0500 Subject: [PATCH] "finished" binary --- num-converter/WebForm1.aspx.cs | 51 ++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/num-converter/WebForm1.aspx.cs b/num-converter/WebForm1.aspx.cs index f2f13f4..07e672f 100644 --- a/num-converter/WebForm1.aspx.cs +++ b/num-converter/WebForm1.aspx.cs @@ -22,7 +22,7 @@ namespace num_converter int BinaryNum; int OctalNum; int Base1 = 1; - + //dec to binary if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 2) { DecimalNum = Convert.ToInt32(txtInput.Text); @@ -34,7 +34,7 @@ namespace num_converter } lblResult.Text = Result; } - + //dec to oct if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 3) { @@ -46,10 +46,10 @@ namespace num_converter DecimalNum /= 8; } - lblResult.Text = "0x" + Result; + lblResult.Text = "Oo" + Result; } - + //dec to hex if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 4) { DecimalNum = Convert.ToInt32(txtInput.Text); @@ -63,8 +63,9 @@ namespace num_converter DecimalNum /= 16; } - lblResult.Text = Result; + lblResult.Text = "0x" + Result; } + //bin to dec if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 1) { BinaryNum = Convert.ToInt32(txtInput.Text, 2); @@ -72,6 +73,46 @@ 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) + { + Remainder = DecimalNum % 2; + DecimalNum /= 2; + Result = Remainder.ToString() + Result; + } + + lblResult.Text = "Oo" + 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;*/ + + } } protected void btnClear_Click(object sender, EventArgs e)