Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

c# - How to save string to text file with ASCII encoding in Windows store apps/WinRT?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...