13

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 ?

Erlaunis
  • 1,433
  • 6
  • 32
  • 50

1 Answers1

28

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);
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • I've already tried with this method but I forgot the brackets and it didn't work. Now it's working perfectly ! Thanks ! :) – Erlaunis Jan 01 '16 at 13:37
  • @Erlaunis Glad to help. – Farhad Jabiyev Jan 01 '16 at 13:45
  • 2
    Good answer. Actually, the last one is the only one that involves ASCII. The first three all take Unicode/UTF-16 code units. And, of course, the results are all Unicode/UTF-16 code units (`char` is one code unit; `string` is a counted sequence of code units). It's important to know which character set and encoding your are using. ASCII is rare. – Tom Blodget Jan 01 '16 at 17:57