I have the problem that I must read Integers from a database. The varibles are returned as Strings and can be "", "0", "1", "2" or "3" (these are the ones I’ve seen so far).
So using the standard atoi function doesn’t help because then I can’t differentiate between "" and "0".
Does somebody have a good solution to solve this?
Regards,
Stefan
EDIT:
“”, “0”, “1”, “2” or “3” aren’t the only possibilities I have seen so far… It is also possible that somebody writes “Hello World” in the field!!!
If performance is an issue in your case, I propose the followings:
Solution 1:
Solution 2:
I suggest to use the first solution because, if input starts invalid content(not an integer), the atoi solution fails (atoi returns 0 on fail).
Although, if you the values can be only “”,”0″,”1″,”2″,”3″, the second solution should work too.