If the string is one character long, you can just index it:
char *s = "\n";
int ascii = s[0];
However, if you are on a system where the character set used is not ASCII, the above will not give you an ASCII value. If you need to make sure your code runs on such rare machines, you can build yourself an ASCII table and use that.
If on the other hand, you have two characters, i.e.,
char *s = "\\n";
then you can do something like this:
char c;
c = s[0];
if (c == '\\') {
c = s[1]; /* assume s is long enough */
switch (c) {
case 'n': return '\n'; break;
case 't': return '\t'; break;
...
default: return c;
}
}
The above assumes that your current compiler knows what '\n'
means. If it doesn't, then you can still do it. For finding out how to do so, and a fascinating story, see Reflections on Trusting Trust by Ken Thompson.