Char 2 Int
Char 2 Int
Hello,
can anyone tell me how to convert a Charakter to an Integer ??
Mabey somebody has a C function for it ...
can anyone tell me how to convert a Charakter to an Integer ??
Mabey somebody has a C function for it ...
-
- Member
- Posts: 132
- Joined: Wed Nov 03, 2004 12:00 am
- Location: Austria
- Contact:
Re: Char 2 Int
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;}
}
(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;}
}
- carbonBased
- Member
- Posts: 382
- Joined: Sat Nov 20, 2004 12:00 am
- Location: Wellesley, Ontario, Canada
- Contact:
Re: Char 2 Int
char c;
int i = (int)c
int i = (int)c
-
- Member
- Posts: 132
- Joined: Wed Nov 03, 2004 12:00 am
- Location: Austria
- Contact:
Re: Char 2 Int
carbonBased
yours is also correct but you only get the int value of the ascii code !
yours is also correct but you only get the int value of the ascii code !
- carbonBased
- Member
- Posts: 382
- Joined: Sat Nov 20, 2004 12:00 am
- Location: Wellesley, Ontario, Canada
- Contact:
Re: Char 2 Int
I realize that. My point was simply that, from the question, it can be interpreted more then one way.
--Jeff
--Jeff
-
- Member
- Posts: 132
- Joined: Wed Nov 03, 2004 12:00 am
- Location: Austria
- Contact:
Re: Char 2 Int
jep
-
- Member
- Posts: 31
- Joined: Sun Jun 26, 2005 11:00 pm
- Location: Toronto, Ontario, Canada
- Contact:
Re: Char 2 Int
Let c = the char.
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.
And for people like me who want this in ASM....
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.
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
-- James Vaughan
Re: Char 2 Int
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
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.
-
- Member
- Posts: 132
- Joined: Wed Nov 03, 2004 12:00 am
- Location: Austria
- Contact:
Re: Char 2 Int
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!
I've only prevented to convert numbers to an int, and not any other character like an 'B' or something else!