public string Convert(string InputVal) { ConvertorClass convertorClass = new ConvertorClass(); convertorClass.InputVal = InputVal; String num = InputVal; // Empty string validation if (string.IsNullOrEmpty(convertorClass.InputVal)) { convertorClass.ErrorText = "Error: Invalid input: Entered string is empty, please enter valid binary string"; return(convertorClass.ErrorText); } int dec_value = 0; // Initializing base value to 1, // i.e 2^0 int base1 = 1; int strLen = convertorClass.InputVal.Length; for (int i = strLen - 1; i >= 0; i--) { if (convertorClass.InputVal[i] == '1') { dec_value += base1; } base1 = base1 * 2; } if (dec_value == 0) { convertorClass.ErrorText = "Error: Invalid input: Entered string is not a binary string, please enter valid binary string"; return(convertorClass.ErrorText); } convertorClass.OutputVal = dec_value; return("Integer: " + convertorClass.OutputVal); }
private static void DoIt(TcpClient client) { NetworkStream stream = client.GetStream(); StreamReader reader = new StreamReader(stream); StreamWriter writer = new StreamWriter(stream); while (true) { string request = reader.ReadLine(); if (request == "Stop") { break; } else { Console.WriteLine("Request: " + request); string[] values = request.Split(' '); string funcion = (string)values[0]; double _response = 0; double a = Double.Parse(values[1]); ConvertorClass c = new ConvertorClass(); if (funcion == "ToOunces") { _response = c.ConvertToOunces(a); } else if (funcion == "ToGrams") { _response = c.ConvertToGrams(a); } Console.WriteLine("Response: " + _response); writer.WriteLine(_response); writer.Flush(); } } client.Close(); }