333 lines
12 KiB
C#
333 lines
12 KiB
C#
/*
|
|
Base-Converter - This file is a part of Base Converter
|
|
Copyright (C) 2022 Caleb Fontenot <foley2431@gmail.com>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
|
|
namespace WindowsFormsApplication1
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
ToolTip toolTip = new ToolTip();
|
|
toolTip.SetToolTip(autoCompute, "Automatically computes input based on changes made to textbox contents.");
|
|
|
|
}
|
|
int paddingOffset = 0, spacingOffset = 0, i = 0;
|
|
public void compute()
|
|
{
|
|
// 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);
|
|
|
|
|
|
binNum = Convert.ToInt64(binaryTextBox.Text, 2);
|
|
|
|
hexNum = Convert.ToInt64(hexTextBox.Text, 16);
|
|
octalNum = Convert.ToInt64(octalTextBox.Text, 8);
|
|
paddingOffset = int.Parse(paddingOffsetTextBox.Text);
|
|
|
|
}
|
|
catch
|
|
{
|
|
clearTextboxes();
|
|
MessageBox.Show("Invalid number entered into one of the textboxes!");
|
|
}
|
|
|
|
// What does the user want us to convert from?
|
|
if (decimalRadioButton.Checked)
|
|
{
|
|
// The decimal radio button is pressed.
|
|
hexNum = decimalNum;
|
|
binNum = decimalNum;
|
|
octalNum = decimalNum;
|
|
//Refocus the textbox when done.
|
|
currentFocus("decimalTextBox");
|
|
}
|
|
if (octalRadioButton.Checked)
|
|
{
|
|
// The octal radio button is pressed.
|
|
hexNum = octalNum;
|
|
binNum = octalNum;
|
|
decimalNum = octalNum;
|
|
//Refocus the textbox when done.
|
|
currentFocus("octalTextBox");
|
|
|
|
}
|
|
if (hexRadioButton.Checked)
|
|
{
|
|
// The hexadecimal radio button is pressed.
|
|
binNum = hexNum;
|
|
decimalNum = hexNum;
|
|
octalNum = hexNum;
|
|
//Refocus the textbox when done.
|
|
currentFocus("hexTextBox");
|
|
}
|
|
if (binaryRadioButton.Checked)
|
|
{
|
|
// The binary radio button is pressed.
|
|
hexNum = binNum;
|
|
decimalNum = binNum;
|
|
octalNum = binNum;
|
|
//Refocus the textbox when done.
|
|
currentFocus("binaryTextBox");
|
|
}
|
|
// Print output
|
|
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++;
|
|
}
|
|
}
|
|
//debugLabel.Text = sender.GetType().ToString();
|
|
binaryTextBox.Text = binaryString;
|
|
}
|
|
else
|
|
{
|
|
binaryTextBox.Text = Convert.ToString(binNum, 2);
|
|
}
|
|
|
|
hexTextBox.Text = hexNum.ToString("X");
|
|
octalTextBox.Text = Convert.ToString(octalNum, 8);
|
|
decimalTextBox.Text = decimalNum.ToString();
|
|
}
|
|
public void spacing()
|
|
{
|
|
spacingOffset = int.Parse(spacingOffsetTextBox.Text);
|
|
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 == 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.
|
|
{
|
|
builder.Insert(i, " ");
|
|
}
|
|
i--;
|
|
}
|
|
else
|
|
{
|
|
i--;
|
|
}
|
|
}
|
|
binaryTextBox.Text = builder.ToString();
|
|
//builder = null; // null out builder when done
|
|
//binaryTextBox.Text = binaryString;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("Divide by zero");
|
|
}
|
|
}
|
|
private void currentFocus(string focusOn)
|
|
{
|
|
switch (focusOn)
|
|
{
|
|
case "binaryTextBox":
|
|
binaryTextBox.Focus();
|
|
break;
|
|
case "hexTextBox":
|
|
hexTextBox.Focus();
|
|
break;
|
|
case "octalTextBox":
|
|
octalTextBox.Focus();
|
|
break;
|
|
case "decimalTextBox":
|
|
decimalTextBox.Focus();
|
|
break;
|
|
default:
|
|
this.Focus();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void clearTextboxes()
|
|
{
|
|
hexTextBox.Text = "0";
|
|
binaryTextBox.Text = "0";
|
|
octalTextBox.Text = "0";
|
|
decimalTextBox.Text = "0";
|
|
}
|
|
|
|
private void computeButton_Click(object sender, EventArgs e)
|
|
{
|
|
// 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 clearButton_Click(object sender, EventArgs e)
|
|
{
|
|
// Clear the textboxes.
|
|
clearTextboxes();
|
|
|
|
// Reset focus to the textbox that is currently selected to be converted to.
|
|
if (decimalRadioButton.Checked)
|
|
currentFocus("decimalTextBox");
|
|
if (hexRadioButton.Checked)
|
|
currentFocus("hexTextBox");
|
|
if (binaryRadioButton.Checked)
|
|
currentFocus("binaryTextBox");
|
|
if (octalRadioButton.Checked)
|
|
currentFocus("octalTextBox");
|
|
}
|
|
|
|
private void exitButton_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void binaryRadioButton_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
currentFocus("binaryTextBox");
|
|
}
|
|
|
|
private void hexRadioButton_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
currentFocus("hexTextBox");
|
|
}
|
|
|
|
private void decimalRadioButton_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
currentFocus("decimalTextBox");
|
|
}
|
|
|
|
private void octalRadioButton_CheckedChanged(object sender, EventArgs e)
|
|
{
|
|
currentFocus("octalTextBox");
|
|
}
|
|
|
|
private void binaryTextBox_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 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();
|
|
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();
|
|
Thread.Sleep(250);
|
|
spacing();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
} |