限制 C 字符串中十六进制转义序列的长度 [英] Limit the Length of a Hexadecimal Escape Sequence in a C-String
问题描述
在字符串文字中,如何使用可以解释为十六进制数字的文字字符立即跟在十六进制转义序列之后?例如,如果我写这个文字...
In a string literal, how can I follow a hexadecimal escape sequence immediately with a literal character that can be interpreted as a hexadecimal digit? For example, if I write this literal ...
"BlahBlah\x04BlahBlah"
...紧跟在 '4'
之后的 'B'
将被解释为十六进制转义的一部分,因为它是一个有效的十六进制数字.
... the 'B'
immediately following the '4'
will be interpreted as part of the hexadecimal escape, because it is a valid hexadecimal digit.
如果 '4'
被视为十六进制转义的最后一个字符,我如何编写一个字符串文字来表示上面将表示的字符串?
How can I write a string literal that represents the string that the above would represent if the
'4'
were taken as the last character of the hex escape?
推荐答案
正如您所注意到的,当涉及到字符串文字中的十六进制转义序列时,C 非常愚蠢.使用字符串连接修复它,如下所示:
As you noticed, C is pretty dumb when it comes to hex escape sequences in string literals. Fix it by using string concatenation, like this:
"BlahBlah\x04" "BlahBlah"
在这种十六进制转义序列后面永远不要有任何尾随字符是一种很好的做法.始终如本示例中那样结束字符串.
It is good practice to never have any trailing characters behind such a hex escape sequence. Always end the string as in this example.
这篇关于限制 C 字符串中十六进制转义序列的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!