Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...