转换的Uni code到Windows 1252的电子名片 [英] Converting Unicode to Windows-1252 for vCards

查看:224
本文介绍了转换的Uni code到Windows 1252的电子名片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在编写程序的 C#,将分割电子名片(VCF)与多个联系人到单独的文件为每个联系人文件。据我所知,电子名片需要保存为ANSI(1252)对于大多数手机阅读它们。

不过,如果我使用的StreamReader 然后用的StreamWriter 写回打开VCF文件(设置1252为编码格式),所有的特殊字符,如 A æ 0 越来越写成。当然,ANSI(1252)将支持这些字符。我该如何解决这个问题?

编辑:下面是我用它来读取和写入文件的一块code

 私人无效的ReadFile()
{
   StreamReader的sreader =新的StreamReader(sourceVCFFile);
   字符串fullFileContents = sreader.ReadToEnd();
}

私人无效WriteFile的()
{
   StreamWriter的swriter =新的StreamWriter(sourceVCFFile,假的,Encoding.GetEncoding(1252));
   swriter.Write(fullFileContents);
}
 

解决方案

您是正确的假设的Windows 1252支持你上面列出的特殊字符(完整列表见的百科词条)。

 使用(VAR作家=新的StreamWriter(目的地,真实,Encoding.GetEncoding(1252)))
{
    writer.WriteLine(源);
}
 

在我使用code以上就产生这一结果的测试应用程序:

看爽信我可以!,,自动曝光和O

被发现

没有问号。你要为你的时候用的StreamReader

阅读它的编码

编辑: 你应该只可以使用 Encoding.Convert 为UTF-8 VCF文件转换到Windows 1252。无需 Regex.Replace 。下面是我会怎么做:

  //你可能需要考虑一个更好的方法名。
公共字符串ConvertUTF8ToWin1252(字符串源)
{
    编码UTF8 =新UTF8Encoding();
    编码win1252 = Encoding.GetEncoding(1252);

    byte []的输入= source.ToUTF8ByteArray(); //注意使用我的扩展方法
    byte []的输出= Encoding.Convert(UTF8,win1252,输入);

    返回win1252.GetString(输出);
}
 

这里是我如何扩展方法如下:

 公共静态类的StringHelper
{
    //应当指出的是,这种方法只期待UTF-8的输入,
    //所以你应该给它一个更合适的名字。
    公共静态的byte [] ToUTF8ByteArray(此字符串str)
    {
        编码编码=新UTF8Encoding();
        返回encoding.GetBytes(STR);
    }
}
 


另外,你可能会想<一href="http://stackoverflow.com/questions/567138/when-should-i-use-using-blocks-in-c/567325#567325">add 使用 s到你的的ReadFile WriteFile的的方法。

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

解决方案

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:

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


Also you'll probably want to add usings to your ReadFile and WriteFile methods.

这篇关于转换的Uni code到Windows 1252的电子名片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆