int v = IR[currentIRIndex] - '0';
this will give you the literal value
Вопрос
I'm reading in a text file for a simulated compiler, and I'm trying to tokenize all the literals. When I realize it's a number value, I'm trying to store the number. However, since the entire line I'm reading from is a string, I only get the ASCII value for the number (i.e. 0 becomes 48), when I really need the value 0. Is there any way to obtain the literal value from the string/char I'm looking at?
example:
std::string IR = "set 0, read"
int currentIRIndex = 4 // (looking at the 0 char)
IR[currentIRIndex]
is 0 if I call it from the <<
operator, and 48 (the ASCII value of 0) if I assign it to an integer.
Решение
int v = IR[currentIRIndex] - '0';
this will give you the literal value