不能在字符串中嵌入非ascii字符

Cannot embed non-ASCII in string

本文关键字:ascii 字符 字符串 不能      更新时间:2023-10-16

当我尝试在字符串中添加非ascii字符时,g++ (v 4.7.3)给出警告。输出完全出乎意料。'23 00 25 30 ';

#include <stdio.h>
int main(int argc, char *argv[]) {
    char *p = "x03123";
    printf("%02x %02x %02x %02xn", p[0], p[1], p[2], p[3]);
    return 0;
}

I got error

g++ -std=c++11 te2a.cc
te2a.cc: In function ‘int main(int, char**)’:
te2a.cc:5:12: warning: hex escape sequence out of range [enabled by default]
te2a.cc:5:12: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

结果我不得不使用

"x03x30x31x32x33"

"x03""123"

猜我被脚本语言(如perl)宠坏了,在那里有"x03123"是可以的。