By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

code for atoi(ascii to integer)

100+
P: 141
hi,

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

thanks
Nov 2 '06 #1
Share this Question
Share on Google+
11 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 2 '06 #2

P: 3
hi,

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

thanks
following code is working

Code removed, please read posting guidelines
Mar 12 '08 #3

Expert 10K+
P: 11,448
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
Mar 12 '08 #4

100+
P: 121
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.
Mar 12 '08 #5

P: 40
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.
Mar 12 '08 #6

100+
P: 141
Hi,

I already got the solution for this

Expand|Select|Wrap|Line Numbers
  1. char* s="-99909zsddfasd";
  2. int num=0,flag=0;
  3. for(int i=0;i<=strlen(s);i++)
  4. {
  5.     if(s[i] >= '0' && s[i] <= '9')
  6.               num = num * 10 + s[i] -'0';
  7.  
  8.     else if(s[0] == '-' && i==0) 
  9.         flag =1;
  10.     else 
  11.  
  12.         break;
  13.  
  14.  
  15. }
  16. if(flag == 1)
  17. num = num * -1;
  18.  printf("%d",num);
  19.  



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.
Mar 13 '08 #7

P: 3
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?
Mar 13 '08 #8

Banfa
Expert Mod 5K+
P: 8,916
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.
Mar 13 '08 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
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
Mar 13 '08 #10

Expert 10K+
P: 11,448
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
Mar 13 '08 #11

P: 3
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,
Mar 13 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.