Hello All, Can anyone know How do I convert a char to an int in C and C++?
I have checked this <<crappy site removed>> to check how I convert it and its difference to read the value as an ascii code, I have wrote
Code:
[COLOR=var(--highlight-literal)]char[/COLOR][COLOR=var(--highlight-color)] a = [/COLOR][COLOR=var(--highlight-variable)]'a'[/COLOR][COLOR=var(--highlight-color)];[/COLOR][COLOR=var(--highlight-literal)]int[/COLOR] ia = ([COLOR=var(--highlight-literal)]int[/COLOR])a; [COLOR=var(--highlight-comment)]/* note that the int cast is not necessary -- int ia = a would suffice */[/COLOR]
to convert the character '0' -> 0, '1' -> 1, etc,
Code:
[COLOR=var(--highlight-literal)]char[/COLOR][COLOR=var(--highlight-color)] a = [/COLOR][COLOR=var(--highlight-variable)]'4'[/COLOR][COLOR=var(--highlight-color)];[/COLOR][COLOR=var(--highlight-literal)]int[/COLOR] ia = a - [COLOR=var(--highlight-variable)]'0'[/COLOR];
[COLOR=var(--highlight-comment)]/* check here if ia is bounded by 0 and 9 */[/COLOR]
[COLOR=var(--highlight-comment)]
[/COLOR]