how do u convert a char to an int?
-
hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:
This is the C# forum, not the C++. You should try and ask questions in the right place. A char *is* an int, you can do this
char a = 65; // = 'A'
int i = 'A'; // i = 65
int z = 5;
z += a; // z = 70Christian After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
-
hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:
Ok, it depends on what you want to do though. Are you talking about having a character such as '4' which you want the value 4? Or are you talking about what the Graus-man said and want the ASCII value of the character? Here is an example:
#include <stdio.h>
#include <stdlib.h>void main( void )
{
char c = 'A';
char d = '4';
int i = 0;printf( "Character '%c' is %d in ascii.\n", c, c );
printf( "Character '%c' is %d in ascii.\n", d, d );
printf( "Character '%c' is %d as an integer.\n", d, atoi( &d ) );
}That yields the output :
Character 'A' is 65 in ascii.
Character '4' is 52 in ascii.
Character '4' is 4 as an integer.I hope that helps you out! ---- Xian - www.hollowmedia.net
-
hi, i just started to learn c++ at school. so this is a very newbie question...but how do u convert a char into an int?? :confused:
If is a char to int use that Christian Graus said but if String to int, you only need to use the atoi function... Cheers Carlos Antollini.