Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a char from an ASCII Character Code in C#

I'm 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 doesn't work for character codes 1 and 2?

like image 241
Jimbo Avatar asked Aug 05 '10 13:08

Jimbo


People also ask

How do I print ASCII values?

Try this: char c = 'a'; // or whatever your character is printf("%c %d", c, c); The %c is the format string for a single character, and %d for a digit/integer. By casting the char to an integer, you'll get the ascii value.


2 Answers

Two options:

char c1 = '\u0001'; char c1 = (char) 1; 
like image 108
Jon Skeet Avatar answered Sep 21 '22 11:09

Jon Skeet


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]; 
like image 33
Pavel Morshenyuk Avatar answered Sep 20 '22 11:09

Pavel Morshenyuk