1,105,340 Community Members

String to integer conversion

Member Avatar
dannyfang
Newbie Poster
8 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
jwenting
duckman
8,359 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 447 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

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

Member Avatar
thatsalok
Newbie Poster
4 posts since Sep 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

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)
   {
      [B]value *= 10;[/B]
      [I]printf("value *= 10 = %3d, ", value);[/I]
      [B]value += text [ i ] - '0';[/B] /* add value of current digit character */
      [I]printf("value += '%c' - '0' = %3d\n", text [ i ], value);[/I]
   }
   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 )
   {
      [B]int digit = text [ i ] - '0';[/B] /* get value of current digit character */
      [I]printf("value = %3d, %3d * 10 = %3d, ", value, value, value * 10);[/I]
      [B]value = 10 * value + digit;[/B]
      [I]printf("digit = %d, value = %d\n", digit, value);[/I]
   }
   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
*/
Member Avatar
dannyfang
Newbie Poster
8 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
cameronius
Newbie Poster
4 posts since Jan 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

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

Member Avatar
murschech
Junior Poster in Training
60 posts since Dec 2004
Reputation Points: 11 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

itoa is nonstandard; sprintf is standard.

#include <iostream.h> is nonstandard; #include <iostream> is standard.

Member Avatar
jwenting
duckman
8,359 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 447 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

actually itoa is standardised in C++

Member Avatar
cameronius
Newbie Poster
4 posts since Jan 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Siersan
Light Poster
45 posts since Jan 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

itoa is nonstandard; sprintf is standard.

#include <iostream.h> is nonstandard; #include <iostream> is standard.

Dave is correct here, itoa() is not ANSI-C, but supported by most compilers.

Member Avatar
brahle
Newbie Poster
18 posts since Mar 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
iamthwee
Posting Sage
7,066 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 595 [?]
Skill Endorsements: 74 [?]
Featured
 
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:

Member Avatar
hay_man
Newbie Poster
16 posts since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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.

Member Avatar
FC Jamison
Posting Pro in Training
447 posts since Jun 2004
Reputation Points: 16 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
Team Colleague
 
-1
 

Convert the string to a character array and then use atoi

Member Avatar
FC Jamison
Posting Pro in Training
447 posts since Jun 2004
Reputation Points: 16 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
Team Colleague
 
0
 
/* 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
Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article