Data conversion function :: String to Integer - int atoi( char * s )
-
:^) The function atoi works in the following manners with a wide range of different arguents:
[ 0] string: _982 ABCD_, converted integer: _982_ [ 1] string: _+982_, converted integer: _982_ [ 2] string: _-982_, converted integer: _-982_ [ 3] string: _0982_, converted integer: _982_ [ 4] string: _982.93_, converted integer: _982_ [ 5] string: _98.2E4_, converted integer: _98_ [ 6] string: _98w2_, converted integer: _98_ [ 7] string: _9+82_, converted integer: _9_
The cases 1, 2 and 3 are perfectly alright. Cool!!! :zzz: What do you think on the cases 0, 6 and 7. There are non-meaningful non-digit characters are present in the input string. The converted values, which are the first possible digit dequence, are simply meaningless. They are similar to junk. But we can't trap them as they depends on the input string. Huh!!! :doh: Now consider the cases 4 and 5. Here meaningful non-digit characters are present. But still the function has failed to recognize themin somewhat manner. For case 4 somebody would vote for 982 as output but some may go for 983 which one is mathematically correct. :wtf: So in which way to go? It is alway better to implement these kind of data conversion function by the programmer themselves else beware of the buggy behaviour of them. -
:^) The function atoi works in the following manners with a wide range of different arguents:
[ 0] string: _982 ABCD_, converted integer: _982_ [ 1] string: _+982_, converted integer: _982_ [ 2] string: _-982_, converted integer: _-982_ [ 3] string: _0982_, converted integer: _982_ [ 4] string: _982.93_, converted integer: _982_ [ 5] string: _98.2E4_, converted integer: _98_ [ 6] string: _98w2_, converted integer: _98_ [ 7] string: _9+82_, converted integer: _9_
The cases 1, 2 and 3 are perfectly alright. Cool!!! :zzz: What do you think on the cases 0, 6 and 7. There are non-meaningful non-digit characters are present in the input string. The converted values, which are the first possible digit dequence, are simply meaningless. They are similar to junk. But we can't trap them as they depends on the input string. Huh!!! :doh: Now consider the cases 4 and 5. Here meaningful non-digit characters are present. But still the function has failed to recognize themin somewhat manner. For case 4 somebody would vote for 982 as output but some may go for 983 which one is mathematically correct. :wtf: So in which way to go? It is alway better to implement these kind of data conversion function by the programmer themselves else beware of the buggy behaviour of them.Good old atoi() is a "best effort" converter, not a "strict" converter. It is documented to work this way, and most C programmers are now accustomed to it. This behaviour can be useful: it's quite common to be dealing with a C string which contains a number in the initial segment which is then followed by more information, and having atoi() work the way it does means that you don't have to temporarily insert NUL terminator bytes or copy parts of the string (which would be nearly as much work as doing the conversion yourself). Both "best effort" and "strict" approaches to conversion are "right" for different problems. I suggest that if you want "data validation" capabilities, you should code your own function, which may call atoi() to do the actual conversion. HTH, Tim
-
Good old atoi() is a "best effort" converter, not a "strict" converter. It is documented to work this way, and most C programmers are now accustomed to it. This behaviour can be useful: it's quite common to be dealing with a C string which contains a number in the initial segment which is then followed by more information, and having atoi() work the way it does means that you don't have to temporarily insert NUL terminator bytes or copy parts of the string (which would be nearly as much work as doing the conversion yourself). Both "best effort" and "strict" approaches to conversion are "right" for different problems. I suggest that if you want "data validation" capabilities, you should code your own function, which may call atoi() to do the actual conversion. HTH, Tim
wtwhite wrote:
Both "best effort" and "strict" approaches to conversion are "right" for different problems. I suggest that if you want "data validation" capabilities, you should code your own function, which may call atoi() to do the actual conversion.
If you want data validation you are better off using
strtol
and the like (strtoul, strtod, strtoi64, strtoui64)