Refactor. Turn padding and spacing into their own functions.

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2022-10-21 19:31:13 -05:00
parent 0e7dff04e5
commit 321023cd33
23 changed files with 825 additions and 782 deletions

Binary file not shown.

View File

@ -121,7 +121,7 @@ namespace WindowsFormsApplication1
this.hexTextBox.Size = new System.Drawing.Size(261, 20); this.hexTextBox.Size = new System.Drawing.Size(261, 20);
this.hexTextBox.TabIndex = 0; this.hexTextBox.TabIndex = 0;
this.hexTextBox.Text = "0"; this.hexTextBox.Text = "0";
this.hexTextBox.TextChanged += new System.EventHandler(this.hexTextBox_TextChanged); this.hexTextBox.Click += new System.EventHandler(this.hexTextBox_Enter);
// //
// binaryGroupBox // binaryGroupBox
// //
@ -141,7 +141,7 @@ namespace WindowsFormsApplication1
this.binaryTextBox.Size = new System.Drawing.Size(261, 20); this.binaryTextBox.Size = new System.Drawing.Size(261, 20);
this.binaryTextBox.TabIndex = 0; this.binaryTextBox.TabIndex = 0;
this.binaryTextBox.Text = "0"; this.binaryTextBox.Text = "0";
this.binaryTextBox.TextChanged += new System.EventHandler(this.binaryTextBox_TextChanged); this.binaryTextBox.Click += new System.EventHandler(this.binaryTextBox_Enter);
// //
// groupBox2 // groupBox2
// //
@ -161,7 +161,7 @@ namespace WindowsFormsApplication1
this.decimalTextBox.Size = new System.Drawing.Size(261, 20); this.decimalTextBox.Size = new System.Drawing.Size(261, 20);
this.decimalTextBox.TabIndex = 0; this.decimalTextBox.TabIndex = 0;
this.decimalTextBox.Text = "0"; this.decimalTextBox.Text = "0";
this.decimalTextBox.TextChanged += new System.EventHandler(this.decimalTextBox_TextChanged); this.decimalTextBox.Click += new System.EventHandler(this.decimalTextBox_Enter);
// //
// exitButton // exitButton
// //
@ -252,16 +252,16 @@ namespace WindowsFormsApplication1
this.octalTextBox.Size = new System.Drawing.Size(261, 20); this.octalTextBox.Size = new System.Drawing.Size(261, 20);
this.octalTextBox.TabIndex = 0; this.octalTextBox.TabIndex = 0;
this.octalTextBox.Text = "0"; this.octalTextBox.Text = "0";
this.octalTextBox.TextChanged += new System.EventHandler(this.octalTextBox_TextChanged); this.octalTextBox.Click += new System.EventHandler(this.octalTextBox_Enter);
// //
// autoCompute // autoCompute
// //
this.autoCompute.AutoSize = true; this.autoCompute.AutoSize = true;
this.autoCompute.Location = new System.Drawing.Point(16, 310); this.autoCompute.Location = new System.Drawing.Point(16, 310);
this.autoCompute.Name = "autoCompute"; this.autoCompute.Name = "autoCompute";
this.autoCompute.Size = new System.Drawing.Size(138, 17); this.autoCompute.Size = new System.Drawing.Size(170, 17);
this.autoCompute.TabIndex = 8; this.autoCompute.TabIndex = 8;
this.autoCompute.Text = "Automatically compute?"; this.autoCompute.Text = "Compute upon pressing enter?";
this.autoCompute.UseVisualStyleBackColor = true; this.autoCompute.UseVisualStyleBackColor = true;
// //
// autoComputeToolTip // autoComputeToolTip
@ -358,7 +358,7 @@ namespace WindowsFormsApplication1
// debugLabel // debugLabel
// //
this.debugLabel.AutoSize = true; this.debugLabel.AutoSize = true;
this.debugLabel.Location = new System.Drawing.Point(160, 311); this.debugLabel.Location = new System.Drawing.Point(192, 311);
this.debugLabel.Name = "debugLabel"; this.debugLabel.Name = "debugLabel";
this.debugLabel.Size = new System.Drawing.Size(63, 13); this.debugLabel.Size = new System.Drawing.Size(63, 13);
this.debugLabel.TabIndex = 14; this.debugLabel.TabIndex = 14;

View File

@ -39,7 +39,13 @@ namespace WindowsFormsApplication1
} }
int paddingOffset = 0, spacingOffset = 0, i = 0; int paddingOffset = 0, spacingOffset = 0, i = 0;
public void compute()
public void printToBinary(string whatToPrint)
{
binaryTextBox.Text = whatToPrint;
}
public long compute()
{ {
// Define variables // Define variables
long decimalNum = 0, hexNum = 0, binNum = 0, octalNum = 0; long decimalNum = 0, hexNum = 0, binNum = 0, octalNum = 0;
@ -103,49 +109,64 @@ namespace WindowsFormsApplication1
currentFocus("binaryTextBox"); currentFocus("binaryTextBox");
} }
// Print output // Print output
hexTextBox.Text = hexNum.ToString("X");
octalTextBox.Text = Convert.ToString(octalNum, 8);
decimalTextBox.Text = decimalNum.ToString();
return binNum;
}
public string padding(long paddingInput)
{
if (paddingToggle.Checked) // Padding: Add Zeros to the beginning of the string if (paddingToggle.Checked) // Padding: Add Zeros to the beginning of the string
{ {
string binaryString = Convert.ToString(binNum, 2); // Define string to offset string binaryString = Convert.ToString(paddingInput, 2); // Define string to offset
int binarySize = binaryString.Length; //count the length of the string. int binarySize = binaryString.Length; //count the length of the string.
if (paddingOffset > binarySize) if (paddingOffset > binarySize)
{ // Is the decimal offset larger than the size of ths string? { // Is the decimal offset larger than the size of ths string?
while (i != (paddingOffset - binarySize)) while (i <= (paddingOffset - binarySize))
{ {
binaryString = "0" + binaryString; binaryString = "0" + binaryString;
i++; i++;
} }
} }
//debugLabel.Text = sender.GetType().ToString(); //debugLabel.Text = sender.GetType().ToString();
binaryTextBox.Text = binaryString; return binaryString;
} }
else else
{ {
binaryTextBox.Text = Convert.ToString(binNum, 2); return Convert.ToString(paddingInput, 2);
} }
hexTextBox.Text = hexNum.ToString("X");
octalTextBox.Text = Convert.ToString(octalNum, 8);
decimalTextBox.Text = decimalNum.ToString();
} }
public void spacing() public string spacing(string binaryString)
{ {
spacingOffset = int.Parse(spacingOffsetTextBox.Text); spacingOffset = int.Parse(spacingOffsetTextBox.Text);
try try
{ {
if (spacingToggle.Checked) if (spacingToggle.Checked)
{ {
string binaryString = binaryTextBox.Text; // Define string to offset //string binaryString = spacingInput; // Define string to offset
int binarySize = binaryString.Length; //count the length of the string. int binarySize = binaryString.Length; //count the length of the string.
i = binarySize; i = binarySize; //- (spacingOffset + 1);
int spacingCounterOffset = 0;
var builder = new StringBuilder(binaryString); // String builder var builder = new StringBuilder(binaryString); // String builder
/*
if (binarySize % 2 == 0) // compensate for numbers being odd
{
spacingCounterOffset = 0;
}
else
{
spacingCounterOffset = spacingOffset - 1;
}
*/
while (i != 0) while (i != 0)
{ {
if (!(i == binarySize)) // Get rid of trailing space if (!(i == binarySize)) // Get rid of trailing space
{ {
if (i % spacingOffset == 0) // If i mod spacingOffset equals zero, append a space to the offset specified by i. if (i % spacingOffset == 0) // If i mod spacingOffset equals zero, append a space to the offset specified by i.
{ {
builder.Insert(i, " "); builder.Insert((i - spacingCounterOffset), " ");
//spacingCounterOffset++;
} }
i--; i--;
} }
@ -154,7 +175,7 @@ namespace WindowsFormsApplication1
i--; i--;
} }
} }
binaryTextBox.Text = builder.ToString(); return builder.ToString();
//builder = null; // null out builder when done //builder = null; // null out builder when done
//binaryTextBox.Text = binaryString; //binaryTextBox.Text = binaryString;
} }
@ -163,6 +184,7 @@ namespace WindowsFormsApplication1
{ {
MessageBox.Show("Divide by zero"); MessageBox.Show("Divide by zero");
} }
return binaryString;
} }
private void currentFocus(string focusOn) private void currentFocus(string focusOn)
{ {
@ -200,8 +222,10 @@ namespace WindowsFormsApplication1
decimalTextBox.Text = decimalTextBox.Text.Replace(",", ""); decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it // Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", ""); binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
compute();
spacing(); // Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
} }
private void clearButton_Click(object sender, EventArgs e) private void clearButton_Click(object sender, EventArgs e)
@ -245,52 +269,7 @@ namespace WindowsFormsApplication1
currentFocus("octalTextBox"); currentFocus("octalTextBox");
} }
private void binaryTextBox_TextChanged(object sender, EventArgs e) private void binaryTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
//decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
compute();
//spacing();
}
}
private void hexTextBox_TextChanged(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
compute();
spacing();
}
}
private void octalTextBox_TextChanged(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
compute();
spacing();
}
}
private void decimalTextBox_TextChanged(object sender, EventArgs e)
{ {
//debugLabel.Text = sender.GetType().ToString(); //debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox"))) if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
@ -300,9 +279,58 @@ namespace WindowsFormsApplication1
// Remove spacing from binaryTextBox before parsing it // Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", ""); binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
compute(); // Set binary output
Thread.Sleep(250); string paddingString = padding(compute());
spacing(); binaryTextBox.Text = spacing(paddingString);
}
}
private void hexTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
}
private void octalTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
}
private void decimalTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
//decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
} }
} }

View File

@ -112,12 +112,12 @@
<value>2.0</value> <value>2.0</value>
</resheader> </resheader>
<resheader name="reader"> <resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="autoComputeToolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="autoComputeToolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value> <value>17, 17</value>
</metadata> </metadata>
</root> </root>

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" /> <dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms> </dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" /> <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>FLb7o5PmMolOEu475l0smDzDYPLdyAdRY/RzK0kEIiY=</dsig:DigestValue> <dsig:DigestValue>/rV8ETvxmwvaJgmA8G++8CRvC5PaSPtVNGW0eHdPwfI=</dsig:DigestValue>
</hash> </hash>
</dependentAssembly> </dependentAssembly>
</dependency> </dependency>

View File

@ -49,7 +49,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" /> <dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms> </dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" /> <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>RykjKwuxJc8SDlNEdmViyEwnW/WDjfua+galOLDEBps=</dsig:DigestValue> <dsig:DigestValue>6QZaxI7Qf42UjLMkjvkpSc+vkOrsV7rHWYV3m96IzcM=</dsig:DigestValue>
</hash> </hash>
</dependentAssembly> </dependentAssembly>
</dependency> </dependency>

View File

@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" /> <dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms> </dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" /> <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>FLb7o5PmMolOEu475l0smDzDYPLdyAdRY/RzK0kEIiY=</dsig:DigestValue> <dsig:DigestValue>/rV8ETvxmwvaJgmA8G++8CRvC5PaSPtVNGW0eHdPwfI=</dsig:DigestValue>
</hash> </hash>
</dependentAssembly> </dependentAssembly>
</dependency> </dependency>

View File

@ -25,3 +25,18 @@ Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.exe.manifest
Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.application Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.application
Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.exe Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.exe
Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.pdb Z:\home\caleb\Base-Converter\BaseConverter\obj\Debug\BaseConverter.pdb
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\bin\Debug\BaseConverter.exe.config
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\bin\Debug\BaseConverter.exe.manifest
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\bin\Debug\BaseConverter.application
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\bin\Debug\BaseConverter.exe
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\bin\Debug\BaseConverter.pdb
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.csproj.AssemblyReference.cache
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.csproj.SuggestedBindingRedirects.cache
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\WindowsFormsApplication1.Form1.resources
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\WindowsFormsApplication1.Properties.Resources.resources
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.csproj.GenerateResource.cache
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.csproj.CoreCompileInputs.cache
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.exe.manifest
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.application
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.exe
Z:\media\DataEXT4\Documents\Base-Converter\BaseConverter\obj\Debug\BaseConverter.pdb

View File

@ -49,7 +49,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" /> <dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms> </dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" /> <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>RykjKwuxJc8SDlNEdmViyEwnW/WDjfua+galOLDEBps=</dsig:DigestValue> <dsig:DigestValue>6QZaxI7Qf42UjLMkjvkpSc+vkOrsV7rHWYV3m96IzcM=</dsig:DigestValue>
</hash> </hash>
</dependentAssembly> </dependentAssembly>
</dependency> </dependency>