Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an int to an ascii char c# [duplicate]

Tags:

c#

ascii

I have an int and I want to display the associate letter. For example, if the int is "1", I want to display "a".

I have a variable "word" and I want to add this letter in it. This is my code :

word += (i+96).ToString();
Console.WriteLine(word);

But in the console, I have a list of number. I found Encoding.ASCII.GetChars but it wants a byte in parameter. How should I do please ?

like image 296
Erlaunis Avatar asked Jan 01 '16 13:01

Erlaunis


1 Answers

You can use one of these methods to convert number to an ASCII / Unicode / UTF-16 character:

You can use these methods convert the value of the specified 32-bit signed integer to its Unicode character:

char c = (char)65;
char c = Convert.ToChar(65); 

But, ASCII.GetString decodes a range of bytes from a byte array into a string:

string s = Encoding.ASCII.GetString(new byte[]{ 65 });

Keep in mind that, ASCIIEncoding does not provide error detection. Any byte greater than hexadecimal 0x7F is decoded as the Unicode question mark ("?").

So, for your solving your problem you can use one of these methods, for example:

word += (char)(i + 96);
like image 158
Farhad Jabiyev Avatar answered Sep 20 '22 22:09

Farhad Jabiyev