将 Unicode 转换为用于 vCard 的 Windows-1252
- 作者: 风轻云淡288
- 来源: 51数据库
- 2023-02-13
问题描述
我正在尝试用 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); } }
此外,您可能还想添加usings 到您的 ReadFile 和 WriteFile 方法.
Also you'll probably want to add usings to your ReadFile and WriteFile methods.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源