// Method to create a new binary value which can be manipulated private BinaryValue createNewBinary(string convertingFrom) { BinaryValue demoValue = new BinaryValue(); // Set values of the user's input type switch (convertingFrom) { case "unsigned": demoValue.IsUnsigned = true; break; case "signed": demoValue.IsSigned = true; break; case "ones-complement": demoValue.IsOnesComplement = true; break; case "twos-complement": demoValue.IsTwosComplement = true; break; } return(demoValue); }
// Main method (where all the *magic* happens) private void btnConvert_Click(object sender, EventArgs e) { string userInput = this.getUserInput(); // Run initial validation if (this.runBaseValidations(userInput)) { string validatedBinaryInput; // Run further validations if ((!this.radDecimal.Checked) && (!this.radFloat.Checked)) { // Validate the binary input if (validator.ValidateUserInput(userInput, (this.radFloatOut.Checked))) { validatedBinaryInput = userInput; // Get conversion type string[] conversionParamaters = this.getConversionParams(); string convertingFrom, convertingTo; convertingFrom = conversionParamaters[0]; convertingTo = conversionParamaters[1]; // Create new binary object BinaryValue newInput = this.createNewBinary(convertingFrom); newInput.Value = validatedBinaryInput; newInput.Value = this.convertBinaryValue(convertingFrom, convertingTo, newInput.Value); // Output converted value this.txtOutputBinary.Text = newInput.Value; } else { // Input is not valid - stop MessageBox.Show(text: "Oops! Double check your input. Please try again."); this.txtInputBinary.Text = ""; this.txtOutputBinary.Text = ""; } } else { if (this.radDecimal.Checked) { int userInputValue; // Validate decimal input if (int.TryParse(userInput, out userInputValue)) { if ((this.validator.runDecimalValidations(userInputValue, this.getConvertingTo())) && (this.isValidNumber(userInputValue))) { // Get conversion type string convertingTo = this.getConvertingTo(); // Convert value string output = this.convertDecimalValue(convertingTo, userInputValue); // Output converted value this.txtOutputBinary.Text = output; } else { MessageBox.Show(text: "Make sure your value can be represented in the style you have chosen to output to. Please try again."); this.txtInputBinary.Text = ""; this.txtOutputBinary.Text = ""; } } else { MessageBox.Show(text: "Oops! Please make sure you input a decimal value. Please try again."); this.txtInputBinary.Text = ""; this.txtOutputBinary.Text = ""; } } else { double userInputValue; // validate floating point input if (double.TryParse(userInput, out userInputValue)) { if (this.validator.runFloatValidations(userInputValue, this.getConvertingTo())) { // Get conversion type string convertingTo = this.getConvertingTo(); // Convert value bool isActuallyFloat = (Convert.ToString(userInputValue).Contains('.')); string output = ""; if (isActuallyFloat) { output = this.convertFloatValue(convertingTo, userInputValue); } else { output = this.convertDecimalValue(convertingTo, Convert.ToInt32(userInputValue)); } // Output converted value this.txtOutputBinary.Text = output; } else { MessageBox.Show(text: "Make sure your value can be represented in the style you have chosen to output to. Please try again."); this.txtInputBinary.Text = ""; this.txtOutputBinary.Text = ""; } } else { MessageBox.Show(text: "Oops! Please make sure you input a floating point value. Please try again."); this.txtInputBinary.Text = ""; this.txtOutputBinary.Text = ""; } } } } else { MessageBox.Show(text: "Oops! Double check your input. Please try again."); this.txtInputBinary.Text = ""; this.txtOutputBinary.Text = ""; } }