How can I define a character or string constant in C # for ASCII 127?

How to create a char or string constant containing a single ASCII character 127?

// Normal printing character - no problems const char VPIPE = '|'; //error "The expression being assigned to 'DEL' must be constant" const char DEL = new string(127, 1); 

It would also be nice if the constants were strings instead of characters:

 const string VPIPE = "|"; const string DEL = "???"; 

I know that ASCII 127 is not something that you can "type" on the keyboard, but there must be a way to create a string or char constant from it (or use the built-in one, which I didn't find).

+4
source share
4 answers

try it

  const char s = ((char)127); 
+5
source

'\x7F' will do this (and can also be embedded in a string if necessary).

+9
source

Personally, I would use the escape sequence "\ u":

 const char Delete = '\u007f'; 

I'm not interested in the escape sequence "\ x" mentioned elsewhere - it's not so bad in character literals (where a few characters => a compiler error), but it can be annoying for string literals:

 // Tab the output Console.WriteLine("\x9Good line"); Console.WriteLine("\x9Bad line"); 

Assuming you can see the error here, how confident are you that you avoid it when creating a β€œjust quick change”?

Given that I am avoiding this for string literals, I think it makes sense to be consistent and just use "\ u" everywhere that I want to escape the hex value.

+7
source

Nothing. I was too fast on the trigger on this.

 const char DEL = (char)127; 
+2
source

Source: https://habr.com/ru/post/1334006/


All Articles