Jump to content
  • Advertisement
Sign in to follow this  
Ksingh30

C++ char to int.

This topic is 4494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

char c = getAChar();

if( c > '9' || c < '0' )
{
// not a number
}
else
{
int value = c - '0';
}



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!