GPX*_*GPX 6 .net c# unicode character-encoding windows-1252
我正在尝试用C#编写一个程序,它将带有多个联系人的vCard(VCF)文件拆分为每个联系人的单个文件.据我所知,vCard需要保存为ANSI(1252)才能让大多数手机读取它们.
但是,如果我用打开VCF档案StreamReader
,然后将其用回写StreamWriter
(设置1252的编码格式),所有的特殊字符,如å
,æ
和ø
越来越写成?
.当然ANSI(1252)会支持这些字符.我该如何解决?
编辑:这是我用来读写文件的代码片段.
private void ReadFile()
{
StreamReader sreader = new StreamReader(sourceVCFFile);
string fullFileContents = sreader.ReadToEnd();
}
private void WriteFile()
{
StreamWriter swriter = new StreamWriter(sourceVCFFile, false, Encoding.GetEncoding(1252));
swriter.Write(fullFileContents);
}
Run Code Online (Sandbox Code Playgroud)
Kre*_*dns 12
假设Windows-1252支持您在上面列出的特殊字符,您是正确的(有关完整列表,请参阅Wikipedia条目).
using (var writer = new StreamWriter(destination, true, Encoding.GetEncoding(1252)))
{
writer.WriteLine(source);
}
Run Code Online (Sandbox Code Playgroud)
在我的测试应用程序中使用上面的代码产生了这个结果:
Look at the cool letters I can make: å, æ, and ø!
没有问题可以找到.您在阅读时是否设置了编码StreamReader
?
编辑:
您应该只能Encoding.Convert
用于将UTF-8 VCF文件转换为Windows-1252.没必要Regex.Replace
.我将如何做到这一点:
// You might want to think of a better method name.
public string ConvertUTF8ToWin1252(string source)
{
Encoding utf8 = new UTF8Encoding();
Encoding win1252 = Encoding.GetEncoding(1252);
byte[] input = source.ToUTF8ByteArray(); // Note the use of my extension method
byte[] output = Encoding.Convert(utf8, win1252, input);
return win1252.GetString(output);
}
Run Code Online (Sandbox Code Playgroud)
以下是我的扩展方法的外观:
public static class StringHelper
{
// It should be noted that this method is expecting UTF-8 input only,
// so you probably should give it a more fitting name.
public static byte[] ToUTF8ByteArray(this string str)
{
Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(str);
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您可能希望将using
s 添加到您的ReadFile
和WriteFile
方法中.
归档时间: |
|
查看次数: |
9506 次 |
最近记录: |