0

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?

  • @AfzaalAhmadZeeshan not really, I tried to work with that already. The code you proposed checks if my string contains at least one string of the string array, however I need a code that checks if there is a character in the string that is not contained in the string array. For what I know, only this way can I assure that my program wont try to read 3 as a binary number. I want the code to read every character of the string and compare it to every string in the array, and output 'false', if one character fails to match any of the strings in the array. – Randomdud3 Jan 19 '22 at 22:00

0 Answers0