How to save string to text file with ASCII encodin

2020-02-15 06:23发布

I am trying hard to save some string to a text file with ASCII encoding in a win8 app project. Below is some of the code snippet:

StorageFile myfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("test.txt");
await FileIO.WriteTextAsync(myfile, "abcd→1234");

But it ended with UTF-8 encoding.

I've done lots of search online and found nothing.So my question is how to write string to text file with ASCII as the encoding. Thanks!

1条回答
你好瞎i
2楼-- · 2020-02-15 07:06

As far as I know, Windows Store apps cannot use non-Unicode encodings. But if your string only contains characters with codepoints between U+00 and U+7F (0..127) then UTF8 and ASCII are identical, because UTF8 was designed to be ASCII-compatible.

So, just encode it as UTF8 and decode as ASCII.

查看更多
登录 后发表回答