user613326 user613326 - 1 month ago 11
C++ Question

How to convert an ASCII char to its ASCII int value?

I would like to convert a char to its ASCII int value.

I could fill an array with all possible values and compare to that, but it doesn't seems right to me. I would like something like

char mychar = "k"
public int ASCItranslate(char c)
return c

ASCItranslate(k) // >> Should return 107 as that is the ASCII value of 'k'.


The point is
atoi()
won't work here as it is for readable numbers only.

It won't do anything with spaces (ASCII 32).

Answer

Do this:-

char mychar = 'k';
public int ASCItranslate(char c)
return int(c);   

ASCItranslate(k); // >> should return 107 as that is the ascii value of k
Comments