Converting Unicode to Windows-1252 for vCards(将 Unicode 转换为用于 vCard 的 Windows-1252)
问题描述
我正在尝试用 C# 编写一个程序,该程序会将包含多个联系人的 vCard (VCF) 文件拆分为每个联系人的单独文件.我知道大多数手机需要将电子名片保存为 ANSI (1252) 才能读取它们.
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.
但是,如果我使用 StreamReader
打开一个 VCF 文件,然后使用 StreamWriter
(设置 1252 作为编码格式)将其写回,所有特殊字符如 å
、æ
和 ø
被写成 ?
.ANSI (1252) 肯定会支持这些字符.我该如何解决这个问题?
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?
这是我用来读写文件的一段代码.
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);
}
推荐答案
您正确地假设 Windows-1252 支持上面列出的特殊字符(有关完整列表,请参阅 维基百科条目).
You are correct in assuming that Windows-1252 supports the special characters you listed above (for a full list see the Wikipedia entry).
using (var writer = new StreamWriter(destination, true, Encoding.GetEncoding(1252)))
{
writer.WriteLine(source);
}
在我使用上面代码的测试应用程序中,它产生了这个结果:
In my test app using the code above it produced this result:
看看我能写出的很酷的字母:å、æ 和 ø!
找不到问号.使用 StreamReader
读取时是否设置了编码?
No question marks to be found. Are you setting the encoding when your reading it in with StreamReader
?
您应该能够使用 Encoding.Convert
将 UTF-8 VCF 文件转换为 Windows-1252.不需要 Regex.Replace
.这是我的做法:
You should just be able to use Encoding.Convert
to convert the UTF-8 VCF file into Windows-1252. No need for Regex.Replace
. Here is how I would do it:
// 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);
}
这是我的扩展方法的外观:
And here is how my extension method looks:
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);
}
}
此外,您可能还想添加using
s 到您的 ReadFile
和 WriteFile
方法.
Also you'll probably want to add using
s to your ReadFile
and WriteFile
methods.
这篇关于将 Unicode 转换为用于 vCard 的 Windows-1252的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 Unicode 转换为用于 vCard 的 Windows-1252
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01