Added spacing and padding support. (Note of known bug: For some reason, using the spacing combined with the auto compute feature causes an infinite loop, causing the program to crash.

This commit is contained in:
2022-10-10 12:20:13 -05:00
parent 7694424069
commit 1a788a3d9c
21 changed files with 202 additions and 19 deletions

View File

@@ -41,13 +41,21 @@ namespace WindowsFormsApplication1
{
// Define variables
long decimalNum = 0, hexNum = 0, binNum = 0, octalNum = 0;
int paddingOffset = 0,spacingOffset = 0, i = 0;
try
{ // Extract data from text boxes.
decimalNum = long.Parse(decimalTextBox.Text);
hexNum = Convert.ToInt64(hexTextBox.Text, 16);
// Remove spacing from binaryTextBox before parsing it
if (spacingToggle.Checked)
{
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
}
binNum = Convert.ToInt64(binaryTextBox.Text, 2);
octalNum = Convert.ToInt64(octalTextBox.Text, 8);
paddingOffset = int.Parse(paddingOffsetTextBox.Text);
spacingOffset = int.Parse(spacingOffsetTextBox.Text);
}
catch (Exception Ex)
{
@@ -94,12 +102,54 @@ namespace WindowsFormsApplication1
currentFocus("binaryTextBox");
}
// Print output
decimalTextBox.Text = decimalNum.ToString();
if (paddingToggle.Checked) // Padding: Add Zeros to the beginning of the string
{
string binaryString = Convert.ToString(binNum, 2); // Define string to offset
int binarySize = binaryString.Length; //count the length of the string.
if (paddingOffset > binarySize)
{ // Is the decimal offset larger than the size of ths string?
while (i != (paddingOffset - binarySize))
{
binaryString = "0" + binaryString;
i++;
}
}
binaryTextBox.Text = binaryString;
}
else
{
binaryTextBox.Text = Convert.ToString(binNum, 2);
}
try
{
if (spacingToggle.Checked)
{
string binaryString = binaryTextBox.Text; // Define string to offset
int binarySize = binaryString.Length; //count the length of the string.
i = binarySize;
var builder = new StringBuilder(binaryString); // String builder
while (i != 0)
{
if (i % spacingOffset == 0) // If i mod spacingOffset equals zero, append a space to the offset specified by i.
{
builder.Insert(i, " ");
}
i--;
}
binaryTextBox.Text = builder.ToString();
builder = null; // null out builder when done
//binaryTextBox.Text = binaryString;
}
} catch (Exception Ex) {
MessageBox.Show("Divide by zero");
}
hexTextBox.Text = hexNum.ToString("X");
binaryTextBox.Text = Convert.ToString(binNum, 2);
octalTextBox.Text = Convert.ToString(octalNum, 8);
decimalTextBox.Text = decimalNum.ToString();
}
private void currentFocus(string focusOn) {
private void currentFocus(string focusOn)
{
switch (focusOn)
{
case "binaryTextBox":
@@ -197,5 +247,29 @@ namespace WindowsFormsApplication1
if (autoCompute.Checked)
compute();
}
private void paddingToggle_CheckedChanged(object sender, EventArgs e)
{
if (paddingToggle.Checked)
{
paddingOffsetTextBox.ReadOnly = false;
}
else
{
paddingOffsetTextBox.ReadOnly = true;
}
}
private void spacingToggle_CheckedChanged(object sender, EventArgs e)
{
if (spacingToggle.Checked)
{
spacingOffsetTextBox.ReadOnly = false;
}
else
{
spacingOffsetTextBox.ReadOnly = true;
}
}
}
}
}