Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Data conversion function :: String to Integer - int atoi( char * s )

Data conversion function :: String to Integer - int atoi( char * s )

Scheduled Pinned Locked Moved Clever Code
questiondiscussion
3 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akur1
    wrote on last edited by
    #1

    :^) 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.

    W 1 Reply Last reply
    0
    • A akur1

      :^) 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.

      W Offline
      W Offline
      wtwhite
      wrote on last edited by
      #2

      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

      PJ ArendsP 1 Reply Last reply
      0
      • W wtwhite

        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

        PJ ArendsP Offline
        PJ ArendsP Offline
        PJ Arends
        wrote on last edited by
        #3

        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)

        Within you lies the power for good; Use it!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups