【问题标题】:Convert a string's character encoding from windows-1252 to utf-8将字符串的字符编码从 windows-1252 转换为 utf-8
【发布时间】:2011-04-06 14:29:55
【问题描述】:

我已将 Word 文档(docx)转换为 html,转换后的 html 具有 windows-1252 作为其字符编码。在 .Net 中,对于这种 1252 字符编码,所有特殊字符都显示为“�”。此 html 显示在 Rad 编辑器中,如果 html 为 Utf-8 格式,该编辑器将正确显示。

我试过下面的代码,但没有静脉

Encoding wind1252 = Encoding.GetEncoding(1252);  
Encoding utf8 = Encoding.UTF8;  
byte[] wind1252Bytes = wind1252.GetBytes(strHtml);  
byte[] utf8Bytes = Encoding.Convert(wind1252, utf8, wind1252Bytes);  
char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];   
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);  
string utf8String = new string(utf8Chars);

关于如何将 html 转换为 UTF-8 的任何建议?

【问题讨论】:

  • 根据您拥有的项目类型(例如 .NetCore),您可能还需要先安装 Nuget 包 System.Text.Encoding.CodePages 并在类构造函数中使用 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 进行初始化跨度>

标签: c# asp.net


【解决方案1】:

其实问题出在这里

byte[] wind1252Bytes = wind1252.GetBytes(strHtml); 

我们不应该从 html 字符串中获取字节。我尝试了下面的代码,它成功了。

Encoding wind1252 = Encoding.GetEncoding(1252);
Encoding utf8 = Encoding.UTF8;
byte[] wind1252Bytes = ReadFile(Server.MapPath(HtmlFile));
byte[] utf8Bytes = Encoding.Convert(wind1252, utf8, wind1252Bytes);
string utf8String = Encoding.UTF8.GetString(utf8Bytes);


public static byte[] ReadFile(string filePath)      
    {      
        byte[] buffer;   
        FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);  
        try
        {
            int length = (int)fileStream.Length;  // get file length    
            buffer = new byte[length];            // create buffer     
            int count;                            // actual number of bytes read     
            int sum = 0;                          // total number of bytes read    

            // read until Read method returns 0 (end of the stream has been reached)    
            while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                sum += count;  // sum is a buffer offset for next reading
        }
        finally
        {
            fileStream.Close();
        }
        return buffer;
    }

【讨论】:

  • 好的;我认为我没有抓住它-您是说不要从.net字符串中获取字节,而是直接从文件系统中二进制读取它。为什么这行得通? b/c .net 字符串内部是 UTF-16?
  • 我想他想说的是系统语言环境会影响字节,因此它永远不会编码好,因此需要读取真实源以获得真实字节然后转换。
【解决方案2】:

应该这样做:

Encoding wind1252 = Encoding.GetEncoding(1252);
Encoding utf8 = Encoding.UTF8;  
byte[] wind1252Bytes = wind1252.GetBytes(strHtml);
byte[] utf8Bytes = Encoding.Convert(wind1252, utf8, wind1252Bytes);
string utf8String = Encoding.UTF8.GetString(utf8Bytes);

【讨论】:

    【解决方案3】:

    使用Encoding.Convert 方法。详情在Encoding.Convert method MSDN article。

    【讨论】:

    • 感谢您的回答,但我试过了,不知道为什么它不适合我。
    【解决方案4】:

    您打算如何使用生成的 html?我认为解决您的问题的最合适方法是添加 meta 和编码规范。比如:

    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 2018-09-30
      • 1970-01-01
      • 2011-12-08
      • 2011-05-20
      相关资源
      最近更新 更多