'Is there a way to get ascii value of a string representing a special character?
I'm working on a compiler and I simply want to be able to read the literal '\n' as it's ascii value (10) in my language.
But there are actually more escape sequence like
\n and I don't want to attempt to account for all of them by myself when C++ already is fully capable of that.I basically need a way to take the array/string:
const char* s1 = {'\\'/* a simple slash */, 'n', '\0'}; // "\\n"
std::string s2 = "\\x002b"; // {'\\', 'x', '0', '0', '2', 'b', '\0'}
and treat it as the C/C++ compiler would have.
I'd like a std functions to do it or if at runtime I could interact with the C++ compiler and let it do the job, something like this:
char c1 = exec("'%s'", s1); // would return '\n' as one character (ascii 10)
char c2 = std::parse_char_literal(s2); // would return '+' (ascii 43)
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
