Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASCIIEncoding does not support in Windows 8 Store app

I have this code work in desktop app, but it doesn't work in Windows 8 Store app, because System.Text does not have ASCIIEncoding support anymore:

tagdata is a byte array.

ASCIIEncoding.ASCII.GetString(tagdata).Trim();

Should I use UT8Encoding? I just want to convert the byte array into ASCII text.

Thank you.

like image 616
Alvin Avatar asked Dec 27 '22 10:12

Alvin


1 Answers

For a start, I'd suggest using Encoding.ASCII everywhere instead of ASCIIEncoding.ASCII - the latter somewhat implies that the ASCII property is a member of the ASCIIEncoding class, which it's not.

If you know that your byte array is just ASCII text, then you can use Encoding.UTF8 freely, as every character present in ASCII is represented the same way in both UTF-8 and ASCII.

If you want to check the validity first, you just need to check that every byte in the array is less than 128

bool isAscii = tagData.All(b => b < 128);
like image 103
Jon Skeet Avatar answered Jan 11 '23 13:01

Jon Skeet