문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top