user974015
user974015

Reputation: 113

ASCII Extended in C# string

How do I make a string in C# to accept non printable ASCII extended characters like • , cause when I try to put • in a string it just give a blank space or null.

Upvotes: 1

Views: 7852

Answers (3)

cjds
cjds

Reputation: 8426

Extended ASCII is just ASCII with the 8 high bits set to different values.

The problem lies in the fact that no commission has ratified a standard for extended ASCII. There are a lot of variants out there and there's no way to tell what you are using.

Now C# uses UTF-16 encoding which will be different from whichever extended ASCII you are using.

You will have to find the matching Unicode character and display it as follows

string a ="\u2649"  ; //where 2649 is a the Unicode number
Console.write(a) ;  

Alternatively you could find out which encoding your files use and use it like so eg. encoding Windows-1252:

Encoding encoding = Encoding.GetEncoding(1252);

and for UTF-16

Encoding enc = new UnicodeEncoding(false, true, true);

and convert it using

 Encoding.Convert (Encoding, Encoding, Byte[], Int32, Int32)

Details are here

Upvotes: 1

Polity
Polity

Reputation: 15130

.NET strings are UTF-16 encoded, not extended-ascii (whatever that is). By simply adding a number to a character will give you another defined character within the UTF-16 plain set. If you want to see the underlying character as it would be in your extended ASCII encoding you need to convert the newly calculated letter from whatever encoding you are talking about to UTF-16. See: http://msdn.microsoft.com/en-us/library/66sschk1.aspx

Upvotes: 0

Uthistran Selvaraj
Uthistran Selvaraj

Reputation: 1371

Try this.. Convert those charcaters as string as folows.

string equivalentLetter = Encoding.Default.GetString(new byte[] { (byte)letter });

Now, the equivalent letter contains the correct string. I tried this for EURO symbol, it worked.

Upvotes: 0

Related Questions