code for atoi(ascii to integer)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vee10
    New Member
    • Oct 2006
    • 141

    code for atoi(ascii to integer)

    hi,

    please post the code for the atoi (asciitointeger )
    urgent please..

    thanks
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Why would you want to know.

    Anyway it is implementation specific, as long as the function does acts in the correct manor it's internal operations are it's own buisness.

    Comment

    • sonalp
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by vee10
      hi,

      please post the code for the atoi (asciitointeger )
      urgent please..

      thanks
      following code is working

      Code removed, please read posting guidelines

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sonalp
        following code is working

        [ ... ]
        n= n*base + (*str-48);
        No it doesn't; it fails in a horrible way on EBCDIC machines.

        kind regards,

        Jos

        Comment

        • Sick0Fant
          New Member
          • Feb 2008
          • 121

          #5
          I might suggest you look at the relationship between chars and ints (clue: a char IS an int). Then I might suggest setting up an array of chars. I might then suggest that you think about how our numbering system is base-ten.
          (i.e. 124 = 1*10^2 + 2*10^1 + 4*10^0). But, that's just me.

          Comment

          • MACKTEK
            New Member
            • Mar 2008
            • 40

            #6
            Vee10,
            I am surprised by your post. Don't you have any code you can show that give us some idea that you are tyring to figure this out?

            A little info to get you started.

            You already posted that you "know" what it does ie ascii to integer.
            Just remember that it takes the number represented and converts it to an integer.
            For example in MSDN
            s = " -9885 pigs"; /* Test of atoi */
            i = atoi( s );
            printf( "atoi test: \"%s\"; integer: %d\n", s, i );the output is:
            atoi test: " -9885 pigs"; integer: -9885

            Knowing this:
            Its pretty clear that you:
            1. Need to parse the input for valid numerics AND signs (such as minus)
            2. Convert the input into its numerical value.

            How you do that is up to you. But a common method is similar to SickOFant's.

            Comment

            • vee10
              New Member
              • Oct 2006
              • 141

              #7
              Hi,

              I already got the solution for this

              Code:
              char* s="-99909zsddfasd";
              int num=0,flag=0;
              for(int i=0;i<=strlen(s);i++)
              {
              	if(s[i] >= '0' && s[i] <= '9')
              	          num = num * 10 + s[i] -'0';
              	
              	else if(s[0] == '-' && i==0) 
              		flag =1;
              	else 
              			
              		break;
              	
              
              }
              if(flag == 1)
              num = num * -1;
               printf("%d",num);



              Originally posted by MACKTEK
              Vee10,
              I am surprised by your post. Don't you have any code you can show that give us some idea that you are tyring to figure this out?

              A little info to get you started.

              You already posted that you "know" what it does ie ascii to integer.
              Just remember that it takes the number represented and converts it to an integer.
              For example in MSDN
              s = " -9885 pigs"; /* Test of atoi */
              i = atoi( s );
              printf( "atoi test: \"%s\"; integer: %d\n", s, i );the output is:
              atoi test: " -9885 pigs"; integer: -9885

              Knowing this:
              Its pretty clear that you:
              1. Need to parse the input for valid numerics AND signs (such as minus)
              2. Convert the input into its numerical value.

              How you do that is up to you. But a common method is similar to SickOFant's.

              Comment

              • sonalp
                New Member
                • Mar 2008
                • 3

                #8
                num = num * 10 + s[i] -'0';


                This is what same as n = n*base + *str-48 of my code, then what was the prob?
                becoz of pointer? or because of ascii value of 0 which is 48?

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by sonalp
                  num = num * 10 + s[i] -'0';


                  This is what same as n = n*base + *str-48 of my code, then what was the prob?
                  becoz of pointer? or because of ascii value of 0 which is 48?
                  Yes, '0' is the value of the character 0, it does not matter what the character set the platform uses is this is always the case, it is a character constant.

                  However 48 is the decimal value of the ASCII character constant '0', on a non ASCII system '0' will still have the correct value for the calculation to work but 48 may very well not be the correct value.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    If you are using Visual Studio, the source code for all of the C library functionsd is shipped with the product.

                    C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by sonalp
                      num = num * 10 + s[i] -'0';


                      This is what same as n = n*base + *str-48 of my code, then what was the prob?
                      becoz of pointer? or because of ascii value of 0 which is 48?
                      The magic word is 'ASCII' here: not the entire world is ASCII, nor is a byte exactly
                      eight bits, nor is the entire world PCs. Writing portable code is still quite an art
                      and it *is* still a problem, not *was* as you have shown in your previous post.

                      kind regards,

                      Jos

                      Comment

                      • sonalp
                        New Member
                        • Mar 2008
                        • 3

                        #12
                        Originally posted by JosAH
                        The magic word is 'ASCII' here: not the entire world is ASCII, nor is a byte exactly
                        eight bits, nor is the entire world PCs. Writing portable code is still quite an art
                        and it *is* still a problem, not *was* as you have shown in your previous post.

                        kind regards,

                        Jos
                        Definitely to use '0' gives portability

                        Thanks,

                        Comment

                        Working...