there's a function to do just that in the standard library by the name of atoi (ASCII to Integer).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Could anyone show me how the string representation of a number like "456" can be converted to its integer equivalent?
#include <stdio.h>
int main(void)
{
int i, value;
const char text[] = "456";
for (i = 0, value = 0; text [ i ] != '\0'; ++i)
{
<strong>value *= 10;</strong>
<em>printf("value *= 10 = %3d, ", value);</em>
<strong>value += text [ i ] - '0';</strong> /* add value of current digit character */
<em>printf("value += '%c' - '0' = %3d\n", text [ i ], value);</em>
}
return 0;
}
/* my output
value *= 10 = 0, value += '4' - '0' = 4
value *= 10 = 40, value += '5' - '0' = 45
value *= 10 = 450, value += '6' - '0' = 456
*/
[edit]Or another way of looking at it:
#include <stdio.h>
int main(void)
{
int i, value;
const char text[] = "456";
for ( i = 0, value = 0; text [ i ] != '\0'; ++i )
{
<strong>int digit = text [ i ] - '0';</strong> /* get value of current digit character */
<em>printf("value = %3d, %3d * 10 = %3d, ", value, value, value * 10);</em>
<strong>value = 10 * value + digit;</strong>
<em>printf("digit = %d, value = %d\n", digit, value);</em>
}
return 0;
}
/* my output
value = 0, 0 * 10 = 0, digit = 4, value = 4
value = 4, 4 * 10 = 40, digit = 5, value = 45
value = 45, 45 * 10 = 450, digit = 6, value = 456
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
[thread=17706]You're not passing a valid string to atol.[/thread]
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
whilst we're on the subject is there a lib function for integer to string ect....
Yes. itoa is the name of the function. There are corresponding functions for long or double to string. Here's an example.
#include <stdlib.h>
#include <iostream.h>
main()
{ int val;
char st[10]
val = 5 + 10;
itoa(val,st,10) //base 10
cout <<"decimal: "<<st<<endl;
itoa(val,st,16) //hexidecimal
cout <<"hexidecimal: "<< st << endl;
}
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 21
Solved Threads: 1
itoa is nonstandard; sprintf is standard.
#include is nonstandard; #include is standard.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
actually itoa is standardised in C++
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
itoa is nonstandard; sprintf is standard.
#include is nonstandard; #include is standard.
Dave is correct here, itoa() is not ANSI-C, but supported by most compilers.
vegaseat
DaniWeb's Hypocrite
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
Haven't you guys heard for sprintf()? it is used as printf(), just the first element must be a *char. They are defined in header cstdio.
Don't bump age old posts. Let them lie in the grave where they belong. :rolleyes:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
As if bumping an old thread wasn't bad enough, you had to use gets() and atoi() to do it.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953