【问题标题】:Non-Unicode to unicode conversion of a txt filetxt 文件的非 Unicode 到 Unicode 转换
【发布时间】:2016-08-13 12:45:40
【问题描述】:

给定一个包含非 unicode 文本的 txt 文件,我能够将其字符集检测为 1251。现在,我想转换成 unicode。

byte[] bytes1251 = Encoding.GetEncoding(1251).GetBytes(File.ReadAllText("sampleNU.txt"));
String str = Encoding.UTF8.GetString(bytes1251);

这不起作用。

这是 非 unicode 到 unicode 转换的方法吗?

在对 RTF 文件尝试建议的方法后,当我尝试打开输出 RTF 文件时出现以下对话框。请让我知道该怎么做,因为选择 Unicode 不会使其可读或给出预期的文本?

【问题讨论】:

    标签: c# unicode non-unicode


    【解决方案1】:
    // load as charset 1251
    string text = File.ReadAllText("sampleNU.txt", Encoding.GetEncoding(1251));
    
    // save as Unicode
    File.WriteAllText("sampleU.txt", text, Encoding.Unicode);
    

    【讨论】:

    • 感谢您的及时回复!当我使用 RTF 文件尝试您的代码时,我得到了附加的对话框。我还有什么需要做的吗?
    • @Holly - rtf (rich text format) 不是 txt(纯文本)。你真正想做什么?
    • @Holly:您还应该说明创建该对话框的原因 - 我们不知道哪个应用程序正在尝试打开文件。
    • @AlexanderPetrov 对不起,我之前提到了 txt 文件。它需要将 rtf non-unicode 转换为 rtf unicode。
    • @JonSkeet 谢谢乔恩!当我打开输出 rtf 文件时,MS Word 会创建该对话框。
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2017-01-02
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多