How to get char from ASCII character code in C #

Im trying to parse a file in C # that has arrays of fields (strings) separated by ascii character codes 0, 1 and 2 (in Visual Basic 6 you can generate them 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 does not work for character codes 1 and 2?

+59
c # escaping character-encoding ascii
Aug 05 '10 at 13:04 on
source share
3 answers

Two options:

 char c1 = '\u0001'; char c1 = (char) 1; 
+124
Aug 05 '10 at 13:08
source share

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]; 
+33
Aug 05 '10 at 13:09
source share

It is important to note that in C # the character type is stored as Unicode UTF-16.

ASCII equivalent integer to char

 char c = (char)88; 

or

 char c = Convert.ToChar(88) 

From character to ASCII equivalent integer

 int asciiCode = (int)'A'; 

The literal must be equivalent to ASCII. For example:

 string str = "Xสีน้ำเงิน"; Console.WriteLine((int)str[0]); Console.WriteLine((int)str[1]); 

will print

 X 3626 

Advanced ASCII ranges from 0 to 255.

From default UTF-16 literal to character

Use symbol

 char c = 'X'; 

Using Unicode Code

 char c = '\u0058'; 

Using hexadecimal

 char c = '\x0058'; 
+13
Jul 09 '16 at 2:05
source share



All Articles