| | |
String to integer conversion
![]() |
•
•
Join Date: Dec 2004
Posts: 8
Reputation:
Solved Threads: 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
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
•
•
Join Date: Sep 2004
Posts: 4
Reputation:
Solved Threads: 1
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
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
"I think it with certainly going to work"
------------------------
Regard's
Alok Gupta
visit me at http://www.thisisalok.tk
•
•
•
•
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
*/#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
*/•
•
Join Date: Dec 2004
Posts: 8
Reputation:
Solved Threads: 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.
Dave,
Your solution provided the answer to my problem. thanks.
Danny.
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.
"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
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by 1o0oBhP
whilst we're on the subject is there a lib function for integer to string ect....
C Syntax (Toggle Plain Text)
#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; }
itoa is nonstandard; sprintf is standard.
#include <iostream.h> is nonstandard; #include <iostream> 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
![]() |
Similar Threads
- String/Integer C++ Question (C++)
- how to convert a string to integer (HTML and CSS)
Other Threads in the C Forum
- Previous Thread: Completely confused
- Next Thread: C:special Squence
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





