C++ char to int.

Started by
22 comments, last by Boder 18 years ago
whats the best way to convert a character to an equivalent int. for example of the char x= '2'; how can you convert that ton int 2.
Advertisement
char c = getAChar();if( c > '9' || c < '0' ){   // not a number}else{   int value = c - '0';}
sweet thanks.
If you only need single digits converted, you can use something like this:
int char2int( char c ) {  return c - '0';}


No error checking, and it's not very sophisticated, but it works. For anything more complex than that (say, converting a string to an int), use atoi (C-style) or a stringstream (C++ style). But the point is, chars are themselves integers, and are in order starting with '0'. Subtract '0', and you get its integer value.
i would guess something like this:

int a == 2;
char b == 'two';
if (a && b == 2 || 'two')
{
\\process goes here
}

but ima newbie v_v
Quote:Original post by damastaplaya
i would guess something like this:

int a == 2;
char b == 'two';
if (a && b == 2 || 'two')
{
\\process goes here
}

but ima newbie v_v


What, that doesn't make any sense. You used the comparison operator for assignment(should be = instead of ==) and you assigned(or attempted to) three characters(which can't be done) to just a char. Plus that if statement doesn't work at all.
Quote:Original post by damastaplaya
i would guess something like this:

int a == 2;
char b == 'two';
if (a && b == 2 || 'two')
{
\\process goes here
}

but ima newbie v_v


Okay.

I would suggest that if you are unsure of how you can help, you can try make a little test program to try it out. For example, the code you posted will not compile.

And there are more elegant solutions than comparing with all possible values in this case.
Quote:Original post by rip-off
Quote:Original post by damastaplaya
i would guess something like this:

int a == 2;
char b == 'two';
if (a && b == 2 || 'two')
{
\\process goes here
}

but ima newbie v_v


Okay.

I would suggest that if you are unsure of how you can help, you can try make a little test program to try it out. For example, the code you posted will not compile.

And there are more elegant solutions than comparing with all possible values in this case.


Well other than the compare for assignment, it would compile... (Unless you are talking about specifically that [grin].)

Now the problem is that a 'two' character would be really quiet and compile, turning into a big bug, so watch out for that damastaplaya :).

Unless... You have warnings reported 100% of the time like me :).

@OP: Google for "ascii table" and click on any of the first like .....3 matches.

I found these tables really fun and useful, because that's how characters are set up. After you know where a '0' is and other things things turn really logical and simple [wink].
Quote:Original post by damastaplaya
int a == 2;
char b == 'two';
if (a && b == 2 || 'two')
{
\\process goes here
}


Erm.. I don't think there could be any more wrong with this code. There are only two lines that will compile, and those are only brackets.

== is the comparison operators, = is the assignment operator. Don't mix them up. A char variable holds a single character, not a character string. A character string is quoted with "double quotes", a single character is quoted with 'single quotes'. They are not interchangable. Also, he wanted to convert '2' to 2, not "two" to 2. "if( a && b == 2 || 'two' )" doesn't do what you want it to. If a evaluates to true (2 does, so yes) and b == 2 ('two' probably means 't', so no, false) or if 'two' evaluates to true ('t' does, so true), then do something. I just don't know what you're intending to say here. Comments use //forward slashes, not \\backslashes.
what if you needed to do this with double digits. say u had string x=12
and u wanted to conver that to int 12.

This topic is closed to new replies.

Advertisement