将 vCard 的 Unicode 转换为 Windows-1252
我正在尝试用 C# 编写一个程序,它将包含多个联系人的 vCard (VCF) 文件拆分为每个联系人的单独文件。据我所知,vCard 需要保存为 ANSI (1252),大多数手机才能读取它们。
但是,如果我使用 StreamReader
打开 VCF 文件,然后使用 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);
}
I am trying to write a program in C# that will split a vCard (VCF) file with multiple contacts into individual files for each contact. I understand that the vCard needs to be saved as ANSI (1252) for most mobile phones to read them.
However, if I open a VCF file using StreamReader
and then write it back with StreamWriter
(setting 1252 as the Encoding format), all special characters like å
, æ
and ø
are getting written as ?
. Surely ANSI (1252) would support these characters. How do I fix this?
Edit: Here's the piece of code I use to read and write the file.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您假设 Windows-1252 支持上面列出的特殊字符是正确的(有关完整列表,请参阅 维基百科条目)。
在我的测试应用程序中,使用上面的代码产生了这样的结果:
看看我可以写的很酷的字母:å、æ 和 ø!
没有找到问号。使用
StreamReader
读取时是否设置编码?编辑:
您应该能够使用 Encoding.Convert 将 UTF-8 VCF 文件转换为 Windows-1252。不需要
Regex.Replace
。这是我的做法:这是我的扩展方法的外观:
另外,您可能想要 将
using
添加到您的ReadFile
和WriteFile
方法中。You are correct in assuming that Windows-1252 supports the special characters you listed above (for a full list see the Wikipedia entry).
In my test app using the code above it produced this result:
Look at the cool letters I can make: å, æ, and ø!
No question marks to be found. Are you setting the encoding when your reading it in with
StreamReader
?EDIT:
You should just be able to use
Encoding.Convert
to convert the UTF-8 VCF file into Windows-1252. No need forRegex.Replace
. Here is how I would do it:And here is how my extension method looks:
Also you'll probably want to add
using
s to yourReadFile
andWriteFile
methods.