首页 >后端开发 >C++ >如何确定 C# 中字符串的编码?

如何确定 C# 中字符串的编码?

Barbara Streisand
Barbara Streisand原创
2025-01-20 19:14:241047浏览

How Can I Determine a String's Encoding in C#?

确定 C# 中字符串的编码

有没有办法确定 C# 中字符串的编码?例如,如果您有一个文件名字符串,但不确定它是采用 UTF-16 编码还是系统默认编码,本指南演示如何确定其编码。

解决方案:

下面的代码具有以下功能:

  • 检测 UTF-7, UTF-8/16/32(bom、无 bom、小端和大端)
  • 如果未找到 Unicode 编码,则回退到本地默认代码页
  • 检测(高概率)unicode缺少 BOM/签名的文件
  • 在文件内搜索 charset=xyz 和encoding=xyz 以帮助确定编码
  • 可自定义的“taster”参数来控制要检查的字节数
  • 返回编码和解码的文本文件
public Encoding detectTextEncoding(string filename, out String text, int taster = 1000)
{
    byte[] b = File.ReadAllBytes(filename);

    // Check for BOM/signature
    if (b.Length >= 4 && b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xFE && b[3] == 0xFF)
    {
        text = Encoding.GetEncoding("utf-32BE").GetString(b, 4, b.Length - 4);
        return Encoding.GetEncoding("utf-32BE");
    }
    else if (b.Length >= 4 && b[0] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00)
    {
        text = Encoding.UTF32.GetString(b, 4, b.Length - 4); 
        return Encoding.UTF32;
    }
    else if (b.Length >= 2 && b[0] == 0xFE && b[1] == 0xFF)
    {
        text = Encoding.BigEndianUnicode.GetString(b, 2, b.Length - 2);
        return Encoding.BigEndianUnicode;
    }
    else if (b.Length >= 2 && b[0] == 0xFF && b[1] == 0xFE)
    {
        text = Encoding.Unicode.GetString(b, 2, b.Length - 2); 
        return Encoding.Unicode;
    }
    else if (b.Length >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF)
    {
        text = Encoding.UTF8.GetString(b, 3, b.Length - 3); 
        return Encoding.UTF8;
    }
    else if (b.Length >= 3 && b[0] == 0x2b && b[1] == 0x2f && b[2] == 0x76)
    {
        text = Encoding.UTF7.GetString(b, 3, b.Length - 3); 
        return Encoding.UTF7;
    }

    // Check for UTF8 without a BOM/signature
    bool utf8 = false;
    int i = 0;
    while (i < taster - 4)
    {
        if (b[i] <= 0x7F) { i += 1; continue; }
        if (b[i] >= 0xC2 && b[i] < 0xE0 && b[i + 1] >= 0x80 && b[i + 1] < 0xC0) { i += 2; utf8 = true; continue; }
        if (b[i] >= 0xE0 && b[i] < 0xF0 && b[i + 1] >= 0x80 && b[i + 1] < 0xC0 && b[i + 2] >= 0x80 && b[i + 2] < 0xC0) { i += 3; utf8 = true; continue; }
        if (b[i] >= 0xF0 && b[i] < 0xF5 && b[i + 1] >= 0x80 && b[i + 1] < 0xC0 && b[i + 2] >= 0x80 && b[i + 2] < 0xC0 && b[i + 3] >= 0x80 && b[i + 3] < 0xC0) { i += 4; utf8 = true; continue; }
        utf8 = false; break;
    }
    if (utf8 == true)
    {
        text = Encoding.UTF8.GetString(b);
        return Encoding.UTF8;
    }

    // Check for UTF-16 without BOM/signature
    double threshold = 0.1;
    int count = 0;
    for (int n = 0; n < taster; n += 2) if (b[n] == 0) count++;
    if (((double)count) / taster > threshold)
    {
        text = Encoding.BigEndianUnicode.GetString(b);
        return Encoding.BigEndianUnicode; 
    }
    count = 0;
    for (int n = 1; n < taster; n += 2) if (b[n] == 0) count++;
    if (((double)count) / taster > threshold)
    {
        text = Encoding.Unicode.GetString(b);
        return Encoding.Unicode; 
    }

    // Check for "charset=xyz" or "encoding=xyz"
    for (int n = 0; n < taster - 9; n++)
    {
        if ((b[n + 0] == 'c' || b[n + 0] == 'C') && (b[n + 1] == 'h' || b[n + 1] == 'H') && (b[n + 2] == 'a' || b[n + 2] == 'A') && (b[n + 3] == 'r' || b[n + 3] == 'R') && (b[n + 4] == 's' || b[n + 4] == 'S') && (b[n + 5] == 'e' || b[n + 5] == 'E') && (b[n + 6] == 't' || b[n + 6] == 'T') && (b[n + 7] == '='))
        {
            n += 8;
            if (b[n] == '&quot;' || b[n] == '\'') n++;
            int oldn = n;
            while (n < taster &amp;&amp; (b[n] == '_' || b[n] == '-' || (b[n] >= '0' && b[n] <= '9') || (b[n] >= 'a' && b[n] <= 'z') || (b[n] >= 'A' && b[n] <= 'Z'))) n++;
            byte[] nb = new byte[n - oldn];
            Array.Copy(b, oldn, nb, 0, n - oldn);
            try
            {
                string internalEnc = Encoding.ASCII.GetString(nb);
                text = Encoding.GetEncoding(internalEnc).GetString(b);
                return Encoding.GetEncoding(internalEnc);
            }
            catch { break; }
        }
    }

    // Fallback to local default codepage
    text = Encoding.Default.GetString(b);
    return Encoding.Default;
}

以上是如何确定 C# 中字符串的编码?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn