I saw this question : How to convert an ASCII char to its ASCII int value?
The most voted answer (https://stackoverflow.com/a/15999291/14911094) states the solution as :
Just do this:
int(k) But i am having issues with this.
My code is :
std::cout << char(144) << std::endl; std::cout << (int)(char(144)) << std::endl; std::cout << int('É') << std::endl; Now the output comes as :
É -112 -55 Now i can understand the first line but what is happening for the second an the third lines?
Firstly how can some ASCII be negative and secondly how can that be different for the same character.
Also as far as i have tested this is not some random garbage from the memory as this stays same for every time i run the program also :
If i change it to 145 :
æ -111 The output to changes by 1 so as far as i guess this may due to some kind of overflow.
But i cannot get it exactly as i am converting to int and that should be enough(4 bytes) to store the result.
Can any one suggest a solution?
chartype is one byte signed type. I.e. it can keep values -127..128. So, when you dochar(144)it is stored as -112 and when you do (int) it just converts single byte int to Cintwithout sign conversion. PS: Value ofint('É')can differ from 144 because your source code is encoded with different encoding from terminal, probably UTF-8.