How to convert "0" and "1" to false and true How to convert "0" and "1" to false and true asp.net asp.net

How to convert "0" and "1" to false and true


How about:

return (returnValue == "1");

or as suggested below:

return (returnValue != "0");

The correct one will depend on what you are looking for as a success result.


In a single line of code:

bool bVal = Convert.ToBoolean(Convert.ToInt16(returnValue))


If you want the conversion to always succeed, probably the best way to convert the string would be to consider "1" as true and anything else as false (as Kevin does). If you wanted the conversion to fail if anything other than "1" or "0" is returned, then the following would suffice (you could put it in a helper method):

if (returnValue == "1"){    return true;}else if (returnValue == "0"){    return false;}else{    throw new FormatException("The string is not a recognized as a valid boolean value.");}