user3482407
user3482407

Reputation: 319

Encoding dec char instead of octal

I do not understand why

char test = '\032';

converts to

26 dec

'\032' seems to be interpreted as octal, but I want it to be treated as a decimal number.

I think I am confused with the character encoding.

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

Upvotes: 1

Views: 193

Answers (2)

chux
chux

Reputation: 154315

In C, '\octal-digit' begins an octal-escape-sequence. There is no decimal-escape-sequence.

Code could simply use:

  char test = 32;

To assign the value of 32 to a char, code has many options:

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

  // 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

Upvotes: 1

Cecilio Pardo
Cecilio Pardo

Reputation: 1717

The syntax you are using (\0xxx) is for octal. To use decimal, you can just do:

char test = (char)32;

Upvotes: 0

Related Questions