编码十进制字符而不是八进制

Encoding dec char instead of octal

我不明白为什么

char test = '2';

转换为

26 dec

'2'好像解释成八进制,但我想把它当成十进制数。

我想我对字符编码感到困惑。

Can anybody clarify this for me and give me a hint on how to convert it the way I want it?

您使用的语法 (\0xxx) 适用于八进制。要使用小数,你可以这样做:

char test = (char)32;

在 C 中,'\octal-digit' 开始一个 octal-escape-sequence。没有decimal-escape-sequence.

代码可以简单地使用:

  char test = 32;

要将32的值赋给char,代码有很多选项:

  // octal escape sequence
  char test1 = '0';  // \ and then 1, 2 or 3 octal digits
  char test2 = '';

  // hexadecimal escape sequence
  char test3 = '\x20'; // \x and then 1 or more hexadecimal digits

  // integer decimal constant
  char test4 = 32;     // 1-9 and then 0 or more decimal digits

  // integer octal constant
  char test5 = 040;     // 0 and then 0 or more octal digits
  char test6 = 0040;
  char test7 = 00040;

  // integer hexadecimal constant
  char test8 = 0x20;   // 0x or 0X and then 1 or more hexadecimal digits
  char test9 = 0X20;

  // universal-character-name
  char testA = '\u0020';      // \u & 4 hex digits
  char testB = '\U00000020';  // \U & 8 hex digits

  // character constant
  char testC = ' ';   // When the character set is ASCII