如何在 C 中转义或终止转义序列
我有一些字符序列要输入到解码函数中:
例如:(
"\x05three"
是的,这是一个 Pascal 风格的字符串。该函数将长度前缀字符串转换为以 null 结尾的字符串。)
我编写了一些测试用例,其中:
"\x04four"
令我惊讶的是,结果是“Oour”。仔细观察,发现转义序列的规范因为 Visual Studio 允许这样做,我的序列基本上被解释为 \x04f
,这将是 10 进制的 79 (因此我的结果字符串变成“Oour”,79 是'O')
我的解决方案只是分割字符串:
"\x04" "four"
问题:是否有另一种方法来转义或终止转义序列?
I have sequences of characters I'm feeding to a decoding function:
For example:
"\x05three"
(Yes, that's a Pascal-style string. The function translates length-prefixed strings to null-terminated strings.)
I wrote a few test cases, among which:
"\x04four"
And to my surprise, that came out as "Oour". Looking closer, it turns out that the specification on escape sequences for Visual Studio allows that, my sequence is basically interpreted as \x04f
, which would be 79 in base 10 (thus my resulting string becomes "Oour", 79 being 'O')
My solution was simply to split the string:
"\x04" "four"
The question: Is there another way to escape or terminate an escape sequence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,例如,您不能尝试
"\004four"
。实际上,甚至"\04four"
也可能可以,因为f
不是八进制数。Yes, you cant try
"\004four"
for instance. Actually, even"\04four"
will probably do, becausef
is not an octal number.您只需将字符串彼此相邻写入即可:
You just write strings next to each other: