Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将用于vCard的Unicode转换为Windows-1252_C#_.net_Unicode_Character Encoding_Windows 1252 - Fatal编程技术网

C# 将用于vCard的Unicode转换为Windows-1252

C# 将用于vCard的Unicode转换为Windows-1252,c#,.net,unicode,character-encoding,windows-1252,C#,.net,Unicode,Character Encoding,Windows 1252,我正试图在C#中编写一个程序,将包含多个联系人的vCard(VCF)文件拆分为每个联系人的单独文件。我知道vCard需要另存为ANSI(1252),以便大多数手机读取 但是,如果我使用StreamReader打开VCF文件,然后使用StreamWriter将其写回(将1252设置为编码格式),则所有特殊字符,如å、æ和都将被写入?。ANSI(1252)肯定会支持这些字符。我该如何解决这个问题 编辑:下面是我用来读写文件的代码片段 private void ReadFile() { Stre

我正试图在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);
}

假设Windows-1252支持上面列出的特殊字符是正确的(有关完整列表,请参阅)

在我的测试应用程序中,使用上面的代码生成以下结果:

看看我能写的很酷的字母:å、æ和ø

找不到问号。使用
StreamReader
读取时是否设置了编码

编辑: 您应该能够使用
编码。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);
}
下面是我的扩展方法的外观:

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);
    }
}


另外,您可能想

我认为OP问题的关键是您的最后一个问题:确保读取VCF的
StreamReader
设置了1252编码。我在使用
StreamReader
读取文件时没有设置编码。我使用的代码与您的示例基本相同。但是输入的VCF文件是UTF-8格式的。出于某种原因,索尼爱立信的“备份到MS”功能将VCF文件保存为UTF-8格式@卢卡斯:我把一切都搞错了。我使用内置功能备份SE和诺基亚手机上的联系人,猜猜看,两者都保存在UTF-8中!问了这么多问题之后,我真是太糟糕了,我错过了!现在,如果我只是在UTF-8模式下使用StreamReader打开一个VCF文件,然后在UTF-8模式下使用StreamWriter再次保存它,则该文件会保存为保留特殊字符,但是,如果我使用Notepad2打开该文件,则会显示“UTF-8 with Signature”作为编码。我做错什么了吗?@GPX:Wikipedia声明BOM。然后,文章给出了几个可能导致问题的例子。所以基本上,带签名的UTF-8只意味着添加了BOM。@GPX:Notepad2可能只是通过打开它来添加它。如果您手边有一个十六进制编辑器/查看器,您可能希望在运行程序后立即查看文本文件。如果BOM实际上是由.NET添加的,那么您总是可以编写代码来检查前三个字节是否为
0xEF、0xBB、0xBF
,如果是,则删除它们。
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);
    }
}