久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 Unicode 轉(zhuǎn)換為用于 vCard 的 Windows-1252

Converting Unicode to Windows-1252 for vCards(將 Unicode 轉(zhuǎn)換為用于 vCard 的 Windows-1252)
本文介紹了將 Unicode 轉(zhuǎn)換為用于 vCard 的 Windows-1252的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試用 C# 編寫一個程序,該程序會將包含多個聯(lián)系人的 vCard (VCF) 文件拆分為每個聯(lián)系人的單獨文件.我知道大多數(shù)手機需要將電子名片保存為 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(設(shè)置 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);
}

推薦答案

您正確地假設(shè) Windows-1252 支持上面列出的特殊字符(有關(guān)完整列表,請參閱 維基百科條目).

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

在我使用上面代碼的測試應(yīng)用程序中,它產(chǎn)生了這個結(jié)果:

In my test app using the code above it produced this result:

看看我能寫出的很酷的字母:?、? 和 ?!

找不到問號.使用 StreamReader 讀取時是否設(shè)置了編碼?

No question marks to be found. Are you setting the encoding when your reading it in with StreamReader?

您應(yīng)該能夠使用 Encoding.Convert 將 UTF-8 VCF 文件轉(zhuǎn)換為 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 到您的 ReadFileWriteFile 方法.


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

這篇關(guān)于將 Unicode 轉(zhuǎn)換為用于 vCard 的 Windows-1252的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Is there a way to know if someone has bookmarked your website?(有沒有辦法知道是否有人為您的網(wǎng)站添加了書簽?)
Use of Different .Net Languages?(使用不同的 .Net 語言?)
Is there a C# library that will perform the Excel NORMINV function?(是否有執(zhí)行 Excel NORMINV 函數(shù)的 C# 庫?)
Determining an #39;active#39; user count of an ASP.NET site(確定 ASP.NET 站點的“活動用戶數(shù))
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權(quán)列表中選擇 x 個隨機元素(無需替換))
Best way to keep track of current online users(跟蹤當(dāng)前在線用戶的最佳方式)
主站蜘蛛池模板: julia中文字幕久久一区二区 | 欧美日韩在线免费 | 伊人春色在线 | 国产日韩精品一区二区 | 亚洲一区国产 | 欧美中文字幕 | 一级做a爰片性色毛片 | 毛片一级片 | 国产精品久久久久久久久久免费看 | 91激情电影 | 欧美精三区欧美精三区 | 成人av一区二区在线观看 | 久久国产一区二区 | 亚洲综合首页 | 成人久久| 日本电影韩国电影免费观看 | 一级黄色毛片免费 | 91视频.| 一区二区三区四区在线 | 99热首页| 91麻豆精品国产91久久久久久久久 | 国产一级在线 | 久久夜夜| 国产精品久久久久久久久久软件 | 超碰在线播 | 国产在线精品一区二区 | 99久久精品国产麻豆演员表 | 欧美久久一区 | 久久精品成人热国产成 | 亚洲高清视频在线 | 国产精品第2页 | 91久久久久久 | 久久国产精品亚洲 | 亚洲一区不卡 | 亚洲免费精品 | 91精品国产综合久久婷婷香蕉 | 成人超碰在线 | 日日骚视频| 操操日 | 亚洲九色 | 韩国av影院 |