943,982 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 48462
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 13th, 2004
0

String to integer conversion

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dannyfang is offline Offline
8 posts
since Dec 2004
Dec 13th, 2004
0

Re: String to integer conversion

there's a function to do just that in the standard library by the name of atoi (ASCII to Integer).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 13th, 2004
0

Re: String to integer conversion

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thatsalok is offline Offline
4 posts
since Sep 2004
Dec 13th, 2004
0

Re: String to integer conversion

Quote 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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 14th, 2004
0

Re: String to integer conversion

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dannyfang is offline Offline
8 posts
since Dec 2004
Dec 15th, 2004
0

Re: String to integer conversion

whilst we're on the subject is there a lib function for integer to string ect....
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Jan 30th, 2005
0

Re: String to integer conversion

i tried the method above, but the segmentation fault error kept appearing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cameronius is offline Offline
4 posts
since Jan 2005
Jan 30th, 2005
0

Re: String to integer conversion

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 30th, 2005
0

Re: String to integer conversion

Quote 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. }
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
murschech is offline Offline
60 posts
since Dec 2004
Jan 30th, 2005
0

Re: String to integer conversion

itoa is nonstandard; sprintf is standard.

#include <iostream.h> is nonstandard; #include <iostream> is standard.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: Completely confused
Next Thread in C Forum Timeline: C:special Squence





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC