I am currently working on a base n to base n converter and I wanted to avoid unhandled exceptions. The one Im targeting right now is that whenever a number or letter is entered that does not represent a valid value in the desired base (2, 8, 10 or 16), the code breaks. I wanted to avoid this by creating a string array for every possible base and then, before the conversion, check if the input string contains any characters that are not contained in the corresponding string arrays and therefor invalid. The idea would look something like this:
private void btn_Calc5_Click(object sender, EventArgs e)
{
string inputValue = textBox_Base1.Text;
string outputValue;
int inputBase = 10;
int outputBase = 10;
string[] validNumbersBinary = new String[2] { "0", "1" };
if (Convert.ToString(comboBox_Base1.SelectedItem) == "2 (Binary)") { inputBase = 2;}
if (inputBase = 2 && [inputValue contains only characters that are an element of validNumbersBinary]) {inputValueBase10 = Convert.ToInt32(inputValue,inputBase)}
if (Convert.ToString(comboBox_Base2.SelectedItem) == "2 (Binary)") { outputBase=2;}
Convert.ToString(inputValueBase10, outputBase);
textBox_Base2.Text = outputValue;
}
What Im looking for is what i filled in with [inputValue contains only characters that are an element of validNumbersBinary]. Is there a piece of code that can do this?