Browse Source

ahhhh

gigatest
ludwig 1 year ago
parent
commit
434b805297
  1. 115
      num-converter/WebForm1.aspx.cs

115
num-converter/WebForm1.aspx.cs

@ -11,14 +11,13 @@ namespace num_converter
{ {
protected void Page_Load(object sender, EventArgs e) protected void Page_Load(object sender, EventArgs e)
{ {
} }
protected void btnConvert_Click(object sender, EventArgs e) protected void btnConvert_Click(object sender, EventArgs e)
{ {
String Result = string.Empty; String Result = string.Empty;
int Remainder; int Remainder;
int DecimalNum; int decNum;
int BinaryNum; int BinaryNum;
int OctalNum; int OctalNum;
string hexVal = string.Empty; string hexVal = string.Empty;
@ -26,47 +25,43 @@ namespace num_converter
//dec to binary //dec to binary
if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 2) if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 2)
{ {
DecimalNum = Convert.ToInt32(txtInput.Text); decNum = Convert.ToInt32(txtInput.Text);
while (DecimalNum > 0) while (decNum > 0)
{ {
Remainder = DecimalNum % 2; Remainder = decNum % 2;
DecimalNum /= 2; decNum /= 2;
Result = Remainder.ToString() + Result; Result = Remainder.ToString() + Result;
} }
lblResult.Text = Result; lblResult.Text = Result;
} }
//dec to oct //dec to oct
if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 3) if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 3)
{ {
decNum = Convert.ToInt32(txtInput.Text, 8);
DecimalNum = Convert.ToInt32(txtInput.Text); OctalNum = decNum;
while (DecimalNum > 0) Result = Convert.ToString(OctalNum);
/*while (decNum > 0)
{ {
Remainder = DecimalNum % 8; Remainder = decNum % 8;
Result = Remainder.ToString() + Result; Result = Remainder.ToString() + Result;
DecimalNum /= 8; decNum /= 8;
} */
}
lblResult.Text = Result;
lblResult.Text = Result;
} }
//dec to hex //dec to hex
if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 4) if (ddlSourcePosNum.SelectedIndex == 1 && ddlTargetPosNum.SelectedIndex == 4)
{ {
DecimalNum = Convert.ToInt32(txtInput.Text); decNum = Convert.ToInt32(txtInput.Text);
hexVal = DecimalNum.ToString("X"); hexVal = decNum.ToString("X");
/*while (DecimalNum > 0)
{
Remainder = DecimalNum % 16;
string hexDigit = (Remainder < 10)
? Remainder.ToString()
: ((char)('A' + Remainder - 10)).ToString();
Result = hexDigit + Result;
DecimalNum /= 16;
}*/
Result = hexVal; Result = hexVal;
lblResult.Text = Result; lblResult.Text = Result;
} }
//bin to dec //bin to dec
if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 1) if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 1)
{ {
@ -75,45 +70,29 @@ namespace num_converter
lblResult.Text = Result; lblResult.Text = Result;
} }
//binary to dec to oct //binary to dec to oct
if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 3) if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 3)
{ {
BinaryNum = Convert.ToInt32(txtInput.Text, 2); BinaryNum = Convert.ToInt32(txtInput.Text, 2);
DecimalNum = BinaryNum; result = Convert.ToString(BinaryNum, 8)
while (DecimalNum > 0) /*decNum = BinaryNum;
while (decNum > 0)
{ {
Remainder = DecimalNum % 2; Remainder = decNum % 2;
DecimalNum /= 2; decNum /= 2;
Result = Remainder.ToString() + Result; Result = Remainder.ToString() + Result;
} */}
lblResult.Text = Result; lblResult.Text = Result;
} }
//bin to hex //bin to hex
if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 4) if (ddlSourcePosNum.SelectedIndex == 2 && ddlTargetPosNum.SelectedIndex == 4)
{ {
string binaryNum = Convert.ToInt32(txtInput.Text, 2).ToString("X"); string binaryNum = Convert.ToInt32(txtInput.Text, 2).ToString("X");
Result = binaryNum; Result = binaryNum;
lblResult.Text = Result; 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;*/
} }
//octal to decimal //octal to decimal
@ -126,21 +105,22 @@ namespace num_converter
if (ddlSourcePosNum.SelectedIndex == 3 && ddlTargetPosNum.SelectedIndex == 2) if (ddlSourcePosNum.SelectedIndex == 3 && ddlTargetPosNum.SelectedIndex == 2)
{ {
DecimalNum = Convert.ToInt32(txtInput.Text, 8); OctalNum = Convert.ToInt32(txtInput.Text, 8);
Result = Convert.ToString(OctalNum, 2);
while (DecimalNum > 0) /*while (decNum > 0)
{ {
Remainder = DecimalNum % 2; Remainder = decNum % 2;
DecimalNum /= 2; decNum /= 2;
Result = Remainder.ToString() + Result; Result = Remainder.ToString() + Result;
} */}
lblResult.Text = Result; lblResult.Text = Result;
} }
if (ddlSourcePosNum.SelectedIndex == 3 && ddlTargetPosNum.SelectedIndex == 4) if (ddlSourcePosNum.SelectedIndex == 3 && ddlTargetPosNum.SelectedIndex == 4)
{ {
DecimalNum = Convert.ToInt32(txtInput.Text, 8); decNum = Convert.ToInt32(txtInput.Text, 8);
hexVal = DecimalNum.ToString("X"); hexVal = decNum.ToString("X");
Result = hexVal; Result = hexVal;
lblResult.Text = Result; lblResult.Text = Result;
} }
@ -148,28 +128,26 @@ namespace num_converter
if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 1) if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 1)
{ {
hexVal = txtInput.Text; hexVal = txtInput.Text;
DecimalNum = Convert.ToInt32(hexVal, 16); decNum = Convert.ToInt32(hexVal, 16);
Result = Convert.ToString(DecimalNum); Result = Convert.ToString(decNum);
lblResult.Text = Result; lblResult.Text = Result;
} }
if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 2) if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 2)
{ {
hexVal = txtInput.Text; hexVal = txtInput.Text;
DecimalNum = Convert.ToInt32(hexVal, 16); decNum = Convert.ToInt32(hexVal, 16);
Result = Convert.ToString(DecimalNum, 2); Result = Convert.ToString(decNum, 2);
lblResult.Text = Result; lblResult.Text = Result;
} }
if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 3) if (ddlSourcePosNum.SelectedIndex == 4 && ddlTargetPosNum.SelectedIndex == 3)
{ {
hexVal = txtInput.Text; hexVal = txtInput.Text;
DecimalNum = Convert.ToInt32(hexVal, 16); decNum = Convert.ToInt32(hexVal, 16);
Result = Convert.ToString(DecimalNum, 8); Result = Convert.ToString(decNum, 8);
lblResult.Text = Result; lblResult.Text = Result;
} }
} }
protected void btnClear_Click(object sender, EventArgs e) protected void btnClear_Click(object sender, EventArgs e)
@ -178,7 +156,6 @@ namespace num_converter
ddlTargetPosNum.SelectedIndex = 0; ddlTargetPosNum.SelectedIndex = 0;
txtInput.Text = ""; txtInput.Text = "";
lblResult.Text = ""; lblResult.Text = "";
} }
} }
} }
Loading…
Cancel
Save