Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How would one create a char or string constant containing the single character ASCII 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 OK if the constants were strings instead of chars:

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

I know that ASCII 127 isn't something you can 'type' at the keyboard, but there has to be a way to create a string or char constant from it (or use a built-in one that I haven't found).

like image 626
Flipster Avatar asked Nov 28 '22 10:11

Flipster


2 Answers

Personally I would use a "\u" escape sequence:

const char Delete = '\u007f';

I'm not keen on the "\x" escape sequence mentioned elsewhere - it isn't too bad in character literals (where multiple characters => compiler error), but it can be nasty for string literals:

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

Assuming you can see the bug here, how certain are you that you'd have avoided it when making "just a quick change"?

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

like image 158
Jon Skeet Avatar answered Dec 16 '22 01:12

Jon Skeet


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

like image 34
MarkXA Avatar answered Dec 16 '22 01:12

MarkXA