On which character table is 'A' assigned to the value of 17? [closed]
I've been messing with turning characters into integers and I wrote this function to do that. (I know that Unicode could break this code but let's assume we won't use any unicode characters here.)
int ctoint( char fCHAR ) {
int cVALUE = fCHAR-'0';
return cVALUE;
}
When I insert character 'A' to this function, it will return '17', when I insert 'B', 18 ... 'Z' 42 etc. So, what does C take reference when it is converting chars into integers when using this kind of code? Thanks in advance :3
Edit: I tried it with some symbols and it returned -5 for a '+', -3 for a '-', -6 for a '*' and -1 for a '/'.
Edit 2: It seems like the it decrements 48 from the ASCII value of that character. '65-48' is 17 so that is an A and '43-48' is -5 which is + as I tried it in the previous edit. Does anyone know, why 48? I guess because it is the start of numbers so numbers' characters will get the value of themselves (e.g. 48-48 is 0)
Edit 3: Thank you all!!!
Solution 1:
In ASCII, '0'
has code 48 and 'A'
has code 65, which is why 'A'-'0'
evaluates to 17. See the full ASCII table on Wikipedia.