在C语言中以八进制表示法以ASCII打印字符串

时间:2018-11-16 21:28:59

标签: c printf ascii octal

我打开一个文件,获取它的内容,寻找一个正则表达式模式的匹配项,然后将匹配项一一打印出来。有些匹配包含八进制符号,例如。 \012换行。

当我简单地应用printf("%s\n", match);时,它被打印为\012。有没有办法打印出相应的ASCII字符(在这种情况下为换行符)?

1 个答案:

答案 0 :(得分:1)

If match is a string, you can use the function strtol in <stdlib.h> to convert it to an int by specifying base 8:

int num = strtol(match + 1, NULL, 8);

Note that I incremented the pointer to match by 1 since it begins with a "\\" which strtol will stop on.