String to integer conversion

Closed Thread

Join Date: Dec 2004
Posts: 8
Reputation: dannyfang is an unknown quantity at this point 
Solved Threads: 0
dannyfang dannyfang is offline Offline
Newbie Poster

String to integer conversion

 
0
  #1
Dec 13th, 2004
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
Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: String to integer conversion

 
0
  #2
Dec 13th, 2004
there's a function to do just that in the standard library by the name of atoi (ASCII to Integer).
Quick reply to this message  
Join Date: Sep 2004
Posts: 4
Reputation: thatsalok is an unknown quantity at this point 
Solved Threads: 1
thatsalok thatsalok is offline Offline
Newbie Poster

Re: String to integer conversion

 
0
  #3
Dec 13th, 2004
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
------------------------
"I think it with certainly going to work"
------------------------
Regard's
Alok Gupta
visit me at http://www.thisisalok.tk
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer conversion

 
0
  #4
Dec 13th, 2004
Originally Posted by dannyfang
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)
   {
      value *= 10;
      printf("value *= 10 = %3d, ", value);
      value += text [ i ] - '0'; /* add value of current digit character */
      printf("value += '%c' - '0' = %3d\n", text [ i ], value);
   }
   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 )
   {
      int digit = text [ i ] - '0'; /* get value of current digit character */
      printf("value = %3d, %3d * 10 = %3d, ", value, value, value * 10);
      value = 10 * value + digit;
      printf("digit = %d, value = %d\n", digit, value);
   }
   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
*/
Quick reply to this message  
Join Date: Dec 2004
Posts: 8
Reputation: dannyfang is an unknown quantity at this point 
Solved Threads: 0
dannyfang dannyfang is offline Offline
Newbie Poster

Re: String to integer conversion

 
0
  #5
Dec 14th, 2004
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.


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

Danny.
Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: String to integer conversion

 
0
  #6
Dec 15th, 2004
whilst we're on the subject is there a lib function for integer to string ect....
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Quick reply to this message  
Join Date: Jan 2005
Posts: 4
Reputation: cameronius is an unknown quantity at this point 
Solved Threads: 0
cameronius cameronius is offline Offline
Newbie Poster

Re: String to integer conversion

 
0
  #7
Jan 30th, 2005
i tried the method above, but the segmentation fault error kept appearing.
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer conversion

 
0
  #8
Jan 30th, 2005
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: String to integer conversion

 
0
  #9
Jan 30th, 2005
Originally Posted by 1o0oBhP
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.
  1. #include <stdlib.h>
  2. #include <iostream.h>
  3.  
  4. main()
  5. { int val;
  6. char st[10]
  7.  
  8. val = 5 + 10;
  9. itoa(val,st,10) //base 10
  10. cout <<"decimal: "<<st<<endl;
  11. itoa(val,st,16) //hexidecimal
  12. cout <<"hexidecimal: "<< st << endl;
  13. }
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String to integer conversion

 
0
  #10
Jan 30th, 2005
itoa is nonstandard; sprintf is standard.

#include <iostream.h> is nonstandard; #include <iostream> is standard.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC