ASCIIEncoding is not supported in the Windows 8 Store app

This code works for me in a desktop application, but it does not work in a Windows 8 Store application, because System.Text no longer supports ASCIIEncoding :

tagdata is a byte array .

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

Should I use UT8Encoding encoding? I just want to convert an array of bytes to ASCII text.

Thanks.

+4
source share
1 answer

To begin with, I would suggest using Encoding.ASCII everywhere, not ASCIIEncoding.ASCII - the latter somewhat implies that the ASCII property is a member of the ASCIIEncoding class, but this is not so.

If you know that your byte array is just ASCII text, you can freely use Encoding.UTF8 , since every character present in ASCII was equally displayed in both UTF-8 and ASCII.

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

 bool isAscii = tagData.All(b => b < 128); 
+10
source

All Articles