指定十六进制转义字符后跟一个字面上的十六进制数字

huangapple go评论65阅读模式
英文:

Specifying hex-escaped character followed by a literal hex digit

问题

The following program generates the following compiler warnings for str2 and str3 in all compilers I checked:

warning: hex escape sequence out of range

My intent is to declare three two-character long strings:

  • str1: \x1b + K
  • str2: \x1b + E
  • str3: \x1b + 0

But instead, it's parsing the last two as if 1be and 1b0 were the specified hex codes.

This seems to happen any time \x?? is followed by a third perceived hexadecimal digit.

In a string literal, how can I specify a character with \x, followed by another character, if that character is also a hexadecimal digit?

英文:

The following program...

int main () {
    const char str1[] = "\x1bK"; // intent: \x1b followed by 'K'
    const char str2[] = "\x1bE"; // intent: \x1b followed by 'E'
    const char str3[] = "\x1b0"; // intent: \x1b followed by '0'
}

...generates the following compiler warnings for str2 and str3 in all compilers I checked:

warning: hex escape sequence out of range

My intent is to declare three two-character long strings:

  • str1: \x1b + K
  • str2: \x1b + E
  • str3: \x1b + 0

But instead its parsing the last two as if 1be and 1b0 were the specified hex codes.

This seems to happen any time \x?? is followed by a third perceived hexadecimal digit.

In a string literal, how can I specify a character with \x, followed by another character, if that character is also a hexadecimal digit?

I feel like this is a really silly question, but somehow I've never been in this situation before.

答案1

得分: 2

你可以简单地让编译器连接两个独立的字符串文字。

int main () {
    const char str1[] = "\x1b" "K";
    const char str2[] = "\x1b" "E";
    const char str3[] = "\x1b" "0";
}
英文:

You could simply have the compiler concatenate 2 separate string literals.

int main () {
    const char str1[] = "\x1b" "K";
    const char str2[] = "\x1b" "E";
    const char str3[] = "\x1b" "0";
}

huangapple
  • 本文由 发表于 2023年7月5日 01:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定