Char 2 Int

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
stafe
Posts: 22
Joined: Fri Oct 29, 2004 11:00 pm

Char 2 Int

Post by stafe »

Hello,

can anyone tell me how to convert a Charakter to an Integer ??
Mabey somebody has a C function for it ...
blackcatcoder
Member
Member
Posts: 132
Joined: Wed Nov 03, 2004 12:00 am
Location: Austria
Contact:

Re: Char 2 Int

Post by blackcatcoder »

substract 48 from the char and you have the int value !
(48 = 0 in ASCII) !

like this

int char_to_int(char c)
{
if (c>=48 && c<=57) // if you only want to convert values!
{ return (c-48);
}else{return -1;}
}
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Char 2 Int

Post by carbonBased »

char c;
int i = (int)c

:)
blackcatcoder
Member
Member
Posts: 132
Joined: Wed Nov 03, 2004 12:00 am
Location: Austria
Contact:

Re: Char 2 Int

Post by blackcatcoder »

carbonBased

yours is also correct but you only get the int value of the ascii code ! ;-)
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: Char 2 Int

Post by carbonBased »

I realize that. My point was simply that, from the question, it can be interpreted more then one way.

--Jeff
blackcatcoder
Member
Member
Posts: 132
Joined: Wed Nov 03, 2004 12:00 am
Location: Austria
Contact:

Re: Char 2 Int

Post by blackcatcoder »

jep
Phibred
Member
Member
Posts: 31
Joined: Sun Jun 26, 2005 11:00 pm
Location: Toronto, Ontario, Canada
Contact:

Re: Char 2 Int

Post by Phibred »

Let c = the char.

Code: Select all

int number = c - ((int)'0');

Yes blackcatcoder, this exactly what you wrote, but there is no point doing any 'allowence' checking, and for god sakes just use '0' instead of what ever it happens to be, much easier to read. :P

And for people like me who want this in ASM....

Code: Select all

;eax = char -> int after calc
CharToInt:
sub eax, '0' ; wow that was easy.
ret
Last edited by Phibred on Mon Aug 29, 2005 11:00 pm, edited 2 times in total.
It will come from the ashes of the old era.
-- James Vaughan
User avatar
proxy
Member
Member
Posts: 108
Joined: Wed Jan 19, 2005 12:00 am
Contact:

Re: Char 2 Int

Post by proxy »

this reminds me of one of my favorite sarcastic C help sites:

http://www.plethora.net/~seebs/faqs/c-i ... #section-7

that should address this question quite nicely

proxy
Last edited by proxy on Mon Dec 15, 2014 8:34 am, edited 1 time in total.
blackcatcoder
Member
Member
Posts: 132
Joined: Wed Nov 03, 2004 12:00 am
Location: Austria
Contact:

Re: Char 2 Int

Post by blackcatcoder »

Phibred yours is really nice but it's recommend to know that int ('0') is 48 in ASCII ! ;-)

I've only prevented to convert numbers to an int, and not any other character like an 'B' or something else!
Post Reply