954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String to integer conversion

Hi,

I'm a freshman in computer science and a new member to this forum. I'd like to convert the string representation of a number e.g "456" into its integer equivalent i.e. 456.

As for character representation of single digits e.g. "6", I'm able to convert it to its integer equivalent by doing:
char myChar = "6";
int asciiVal = myChar - '0'; //produces the integer 6 now.

Could anyone show me how the string representation of a number like "456" can be converted to its integer equivalent?

Thanks
Danny

dannyfang
Newbie Poster
8 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

there's a function to do just that in the standard library by the name of atoi (ASCII to Integer).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Use it this Way

char a[]="123";
char b[]="123.66";
//for interger
int i=atoi(a);

//for Long

long l=atol(a);

//for double

float d=atof(b);

Hope this will help you

thatsalok
Newbie Poster
4 posts since Sep 2004
Reputation Points: 10
Solved Threads: 1
 
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
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Dave, Alok & Jwenting,

Thanks very much for the help. In fact I forgot to mention earlier when posting this problem that I'm not suppose to use built-in functions like atoi or atol for such conversions. Nevertheless, thanks to everyone for their help.
:D

Dave,
Your solution provided the answer to my problem. thanks. :D

Danny.

dannyfang
Newbie Poster
8 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

whilst we're on the subject is there a lib function for integer to string ect....

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

i tried the method above, but the segmentation fault error kept appearing.

cameronius
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

[thread=17706]You're not passing a valid string to atol.[/thread]

Dave Sinkula
long time no c
Team Colleague
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
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

actually itoa is standardised in C++

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
[thread=17706]You're not passing a valid string to atol.[/thread]


i changed the code of that section.

while (passkey[x] != '\0')    // While the string isn't at the end...
    {
        passkey[x] = (int)passkey[x];    // Transform the char to int
        x++;
    }


Does that help? Does that make the code better?

Thanks so far.

cameronius
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
actually itoa is standardised in C++


Not in my copy of the standard. What section and page are you referring to?Does that help? Does that make the code better?
The loop doesn't do anything useful, the string that comes out will be the same string that goes in. Try running this and ignore the warning you might get about conversion from int to char.

#include <iostream>

using namespace std;

int main()
{
  char s[] = "A test string";

  for (int i = 0; s[i] != '\0'; i++)
    s[i] = int(s[i]);

  cout<< s <<endl;
}
Siersan
Light Poster
45 posts since Jan 2005
Reputation Points: 12
Solved Threads: 2
 

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
Moderator
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.

brahle
Newbie Poster
18 posts since Mar 2006
Reputation Points: 11
Solved Threads: 0
 
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
 

I too am stuck on this problem of converting a string to its integer equivalent. My lecturer suggested that Horner's method can be adapted to convert string characters into their integer values. E.g. "4123" becomes 4123. Would you have any idea on how to implement this or are there any alternative methods to complete such a conversion.
I guess one could extract each char in the string then convert that and so on, just have a loop?
Hope you guys can help.

hay_man
Newbie Poster
16 posts since Jun 2006
Reputation Points: 38
Solved Threads: 0
 

Convert the string to a character array and then use atoi

FC Jamison
Posting Pro in Training
Team Colleague
446 posts since Jun 2004
Reputation Points: 92
Solved Threads: 21
 
/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char szInput [256];
  printf ("Enter a number: ");
  gets ( szInput );
  i = atoi (szInput);
  printf ("Value entered is %d, and its double %d",i,i*2);
  return 0;
}

Output:
Enter a number: 73
Value entered is 73, and its double 146
FC Jamison
Posting Pro in Training
Team Colleague
446 posts since Jun 2004
Reputation Points: 92
Solved Threads: 21
 

As if bumping an old thread wasn't bad enough, you had to use gets() and atoi() to do it.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You