简体   繁体   English

如何将字符转换为关联的转义序列?

[英]How to convert a char to the associated escape sequence?

I need to transform escape sequences contained in non-literal strings (so that they are split in more characters, the first one being \\ and the others being just n , r or even octal codes etc.) to the actual character represented by that escape sequence.我需要将包含在非文字字符串中的转义序列(以便它们分成更多字符,第一个是\\ ,其他的只是nr甚至八进制代码等)转换为该转义符表示的实际字符序列。

My first thought was it could be possible using a macro (like #define to_escape(x) '\\(x)' which doesn't work), but I wonder if there actually is a way like that and if there are better ways.我的第一个想法是可以使用宏(比如#define to_escape(x) '\\(x)' ,它不起作用),但我想知道是否真的有这样的方法,是否有更好的方法。

Of course I could use a simple switch statement for escapes like \\a , \\f and similars, but then how would I handle all the octal and hexadecimal escapes?当然,我可以使用简单的 switch 语句来进行转义,例如\\a\\f和类似的转义,但是我将如何处理所有八进制和十六进制转义?

you can go for this byte allocation你可以去这个字节分配

\'  single quote            byte 0x27 in ASCII encoding
\"  double quote            byte 0x22 in ASCII encoding
\?  question mark           byte 0x3f in ASCII encoding
\\  backslash               byte 0x5c in ASCII encoding
\a  audible bell            byte 0x07 in ASCII encoding
\b  backspace               byte 0x08 in ASCII encoding
\f  form feed - new page    byte 0x0c in ASCII encoding
\n  line feed - new line    byte 0x0a in ASCII encoding
\r  carriage return         byte 0x0d in ASCII encoding
\t  horizontal tab          byte 0x09 in ASCII encoding
\v  vertical tab            byte 0x0b in ASCII encoding

check if their addresses match their hexa-addresses and you can retrieve the following character.检查他们的地址是否与他们的六进制地址匹配,您可以检索以下字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM