八进制和十六进制字符规范Octal and Hexadecimal Character Specifications

序列 \ooo 表示可以将 ASCII 字符集中的任何字符指定为三位数八进制字符代码。The sequence \ooo means you can specify any character in the ASCII character set as a three-digit octal character code. 八进制整数的数字值用于指定所需字符或宽字符的值。The numerical value of the octal integer specifies the value of the desired character or wide character.

同样,序列 \xhhh 可使用户将任何 ASCII 字符指定为十六进制字符代码。Similarly, the sequence \xhhh allows you to specify any ASCII character as a hexadecimal character code. 例如,可以将 ASCII 退格符指定为常规 C 转义序列 (\b),或者也可以将其编码为 \010(八进制)或 \x008(十六进制)。For example, you can give the ASCII backspace character as the normal C escape sequence (\b), or you can code it as \010 (octal) or \x008 (hexadecimal).

在八进制转义序列中只能使用 0 到 7 的数字。You can use only the digits 0 through 7 in an octal escape sequence. 八进制转义序列绝不能长于三位且不能由第一个不是八进制数字的字符结尾。Octal escape sequences can never be longer than three digits and are terminated by the first character that is not an octal digit. 虽然不需要使用所有三个数字,但必须至少使用其中一个。Although you do not need to use all three digits, you must use at least one. 例如,ASCII 退格符的八进制表示形式是 \10,字母 A 的八进制表示形式是 \101,如 ASCII 图表中所提供。For example, the octal representation is \10 for the ASCII backspace character and \101 for the letter A, as given in an ASCII chart.

同样,对十六进制转义序列必须至少使用一个数字,但是您可以忽略第二个和第三个数字。Similarly, you must use at least one digit for a hexadecimal escape sequence, but you can omit the second and third digits. 因此,可以将退格符的十六进制转义序列指定为 \x8、\x08 或 \x008。Therefore you could specify the hexadecimal escape sequence for the backspace character as either \x8, \x08, or \x008.

八进制或十六进制转义序列的值必须在字符常量的类型 unsigned char 和宽字符常量的类型 wchar_t 的可表示值的范围内。The value of the octal or hexadecimal escape sequence must be in the range of representable values for type unsigned char for a character constant and type wchar_t for a wide-character constant. 有关宽字符常量的信息,请参阅多字节和宽字符See Multibyte and Wide Characters for information on wide-character constants.

不同于八进制转义常量,转义序列中的十六进制数字的数量不受限制。Unlike octal escape constants, the number of hexadecimal digits in an escape sequence is unlimited. 十六进制转义序列在第一个不是十六进制数字的字符处结尾。A hexadecimal escape sequence terminates at the first character that is not a hexadecimal digit. 由于十六进制数字包含字母 a 到 f,因此必须小心谨慎,确保转义序列在预期的数字处终止。Because hexadecimal digits include the letters a through f, care must be exercised to make sure the escape sequence terminates at the intended digit. 为了避免混淆,您可以将八进制或十六进制字符定义放入宏定义:To avoid confusion, you can place octal or hexadecimal character definitions in a macro definition:

#define Bell '\x07'  

对于十六进制值,可以拆开字符串以清楚地显示正确的值:For hexadecimal values, you can break the string to show the correct value clearly:

"\xabc"    /* one character  */  
"\xab" "c" /* two characters */  

另请参阅See Also

C 字符常量C Character Constants