views:

140

answers:

2

Im trying to parse a file in c# that has field (string) arrays separated by ascii character codes 0, 1 and 2 (in Visual Basic 6 you can generate these by using Chr(0) or Chr(1) etc.)

I know that for character code 0 in c# you can do the following:

char separator = '\0';

But this doesnt work for character codes 1 and 2?

+5  A: 

Two options:

char c1 = '\u0001';
char c1 = (char) 1;
Jon Skeet
What about char sep = '\x32'; ?
ngoozeff
@ngoozeff: I dislike `\x` for various reasons, and wouldn't suggest using it.
Jon Skeet
LOL - It makes me depressed to give you even MORE points Jon, but thanks for the answer anyway :P
Jimbo
+2  A: 

You can simply write:

char c = (char) 2;

or

char c = Convert.ToChar(2);

or more complex option for ASCII encoding only

char[] characters = System.Text.Encoding.ASCII.GetChars(new byte[]{2});
char c = characters[0];
Pavel Morshenyuk
Given that we're talking about two specific values which are less than 128, the latter option seems unnecessarily long-winded. Unicode was designed to match ASCII.
Jon Skeet
Yes, you're right. It is just another option. Thank you.
Pavel Morshenyuk