vico
vico

Reputation: 18191

ASCII table and String

Trying to build string by using ASCII table. I was expecting to get black square located in 219 position, but got character U. Why?

static void Main(string[] args)
{
    string d = "";
    d += (char)219;
    Console.WriteLine(d);
    Console.ReadLine();
}

Upvotes: 0

Views: 708

Answers (2)

Cepheus
Cepheus

Reputation: 589

ASCII is a 7 bit encoding and does neither contain a black square, nor does it contain a character with value 219 (the highest ASCII value is 127). To get a black square, you can use a Unicode literal:

d += (char)'\u25A0';

See https://unicode-table.com/en/search/?q=black+square for the Unicode character and https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char for the Unicode literal syntax.

Upvotes: 2

D-Shih
D-Shih

Reputation: 46239

(char)219; isn't convert to ASCII.

If you want to let the number be ASCII, you can try to use Encoding.ASCII.GetString to get ASCII value.

Encoding.ASCII.GetString(new byte[]{127});

NOTE

Encoding.ASCII does not provide error detection. Any byte greater than 127 is decoded as the Unicode question mark ?.

Upvotes: 2

Related Questions