C #에서 문자열 인코딩 결정 있지만 유니 코드 UTF-16

C #에서 문자열 인코딩을 결정하는 방법이 있습니까?

예를 들어 파일 이름 문자열이 있지만 유니 코드 UTF-16 또는 시스템 기본 인코딩으로 인코딩되어 있는지 알 수없는 경우 어떻게 알 수 있습니까?



답변

Utf8Checker는 순수한 관리 코드 에서이 작업을 수행하는 간단한 클래스입니다.
http://utf8checker.codeplex.com

주의 사항 : 이미 지적했듯이 “인코딩 결정”은 바이트 스트림에만 의미가 있습니다. 문자열이 있으면 문자열을 먼저 인식하기 위해 인코딩을 이미 알고 있거나 추측 한 방식으로 누군가가 이미 인코딩 한 것입니다.


답변

아래 코드에는 다음과 같은 기능이 있습니다.

  1. UTF-7, UTF-8 / 16 / 32 감지 또는 감지 시도 (bom, no bom, little & big endian)
  2. 유니 코드 인코딩이 없으면 로컬 기본 코드 페이지로 폴백합니다.
  3. BOM / 서명이없는 유니 코드 파일을 탐지합니다 (확률이 높은).
  4. 인코딩을 결정하는 데 도움이되도록 파일 내에서 charset = xyz 및 encoding = xyz를 검색합니다.
  5. 처리를 저장하기 위해 파일을 정의 할 수 있습니다 (정의 가능한 바이트 수).
  6. 인코딩 및 디코딩 된 텍스트 파일이 반환됩니다.
  7. 효율성을위한 순수한 바이트 기반 솔루션

다른 사람들이 말했듯이 완벽한 솔루션은 없으며 (전세계에서 사용되는 다양한 8 비트 확장 ASCII 인코딩을 쉽게 구별 할 수는 없지만) 개발자가 사용자에게 제시하면 특히 충분할 수 있습니다. 여기에 표시된 대체 인코딩 목록 : 각 언어의 가장 일반적인 인코딩은 무엇입니까?

인코딩의 전체 목록은 다음을 사용하여 찾을 수 있습니다. Encoding.GetEncodings();

// Function to detect the encoding for UTF-7, UTF-8/16/32 (bom, no bom, little
// & big endian), and local default codepage, and potentially other codepages.
// 'taster' = number of bytes to check of the file (to save processing). Higher
// value is slower, but more reliable (especially UTF-8 with special characters
// later on may appear to be ASCII initially). If taster = 0, then taster
// becomes the length of the file (for maximum reliability). 'text' is simply
// the string with the discovered encoding applied to the file.
public Encoding detectTextEncoding(string filename, out String text, int taster = 1000)
{
    byte[] b = File.ReadAllBytes(filename);

    //////////////// First check the low hanging fruit by checking if a
    //////////////// BOM/signature exists (sourced from http://www.unicode.org/faq/utf_bom.html#bom4)
    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"); }  // UTF-32, big-endian 
    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; }    // UTF-32, little-endian
    else if (b.Length >= 2 && b[0] == 0xFE && b[1] == 0xFF) { text = Encoding.BigEndianUnicode.GetString(b, 2, b.Length - 2); return Encoding.BigEndianUnicode; }     // UTF-16, big-endian
    else if (b.Length >= 2 && b[0] == 0xFF && b[1] == 0xFE) { text = Encoding.Unicode.GetString(b, 2, b.Length - 2); return Encoding.Unicode; }              // UTF-16, little-endian
    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; } // UTF-8
    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; } // UTF-7


    //////////// If the code reaches here, no BOM/signature was found, so now
    //////////// we need to 'taste' the file to see if can manually discover
    //////////// the encoding. A high taster value is desired for UTF-8
    if (taster == 0 || taster > b.Length) taster = b.Length;    // Taster size can't be bigger than the filesize obviously.


    // Some text files are encoded in UTF8, but have no BOM/signature. Hence
    // the below manually checks for a UTF8 pattern. This code is based off
    // the top answer at: /programming/6555015/check-for-invalid-utf8
    // For our purposes, an unnecessarily strict (and terser/slower)
    // implementation is shown at: /programming/1031645/how-to-detect-utf-8-in-plain-c
    // For the below, false positives should be exceedingly rare (and would
    // be either slightly malformed UTF-8 (which would suit our purposes
    // anyway) or 8-bit extended ASCII/UTF-16/32 at a vanishingly long shot).
    int i = 0;
    bool utf8 = false;
    while (i < taster - 4)
    {
        if (b[i] <= 0x7F) { i += 1; continue; }     // If all characters are below 0x80, then it is valid UTF8, but UTF8 is not 'required' (and therefore the text is more desirable to be treated as the default codepage of the computer). Hence, there's no "utf8 = true;" code unlike the next three checks.
        if (b[i] >= 0xC2 && b[i] <= 0xDF && 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] <= 0xF4 && 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;
    }


    // The next check is a heuristic attempt to detect UTF-16 without a BOM.
    // We simply look for zeroes in odd or even byte places, and if a certain
    // threshold is reached, the code is 'probably' UF-16.          
    double threshold = 0.1; // proportion of chars step 2 which must be zeroed to be diagnosed as utf-16. 0.1 = 10%
    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; } // (little-endian)


    // Finally, a long shot - let's see if we can find "charset=xyz" or
    // "encoding=xyz" to identify the encoding:
    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] == '=')) ||
            ((b[n + 0] == 'e' || b[n + 0] == 'E') && (b[n + 1] == 'n' || b[n + 1] == 'N') && (b[n + 2] == 'c' || b[n + 2] == 'C') && (b[n + 3] == 'o' || b[n + 3] == 'O') && (b[n + 4] == 'd' || b[n + 4] == 'D') && (b[n + 5] == 'i' || b[n + 5] == 'I') && (b[n + 6] == 'n' || b[n + 6] == 'N') && (b[n + 7] == 'g' || b[n + 7] == 'G') && (b[n + 8] == '='))
            )
        {
            if (b[n + 0] == 'c' || b[n + 0] == 'C') n += 8; else n += 9;
            if (b[n] == '"' || b[n] == '\'') n++;
            int oldn = n;
            while (n < taster && (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; }    // If C# doesn't recognize the name of the encoding, break.
        }
    }


    // If all else fails, the encoding is probably (though certainly not
    // definitely) the user's local codepage! One might present to the user a
    // list of alternative encodings as shown here: /programming/8509339/what-is-the-most-common-encoding-of-each-language
    // A full list can be found using Encoding.GetEncodings();
    text = Encoding.Default.GetString(b);
    return Encoding.Default;
}

답변

문자열이 어디에서 왔는지에 달려 있습니다. .NET 문자열은 유니 코드 (UTF-16)입니다. 예를 들어 데이터베이스의 데이터를 바이트 배열로 읽는 경우 다른 방법이 될 수 있습니다.

이 CodeProject 기사가 관심을 가질 수 있습니다 . 들어오고 나가는 텍스트의 인코딩 감지

C # 및 .NET 의 Jon Skeet의 문자열 은 .NET 문자열에 대한 훌륭한 설명입니다.


답변

나는 이것이 조금 늦었다는 것을 알고 있습니다.

문자열에는 실제로 인코딩이 없습니다. .NET에서 문자열은 char 객체의 모음입니다. 기본적으로 문자열 인 경우 이미 디코딩되었습니다.

그러나 바이트로 구성된 파일의 내용을 읽고이를 문자열로 변환하려면 파일의 인코딩을 사용해야합니다.

.NET에는 ASCII, UTF7, UTF8, UTF32 등을위한 인코딩 및 디코딩 클래스가 포함되어 있습니다.

이러한 대부분의 인코딩에는 사용 된 인코딩 유형을 구별하는 데 사용할 수있는 특정 바이트 순서 표시가 있습니다.

.NET 클래스 System.IO.StreamReader는 바이트 순서 표시를 읽어 스트림 내에서 사용되는 인코딩을 결정할 수 있습니다.

예를 들면 다음과 같습니다.

    /// <summary>
    /// return the detected encoding and the contents of the file.
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="contents"></param>
    /// <returns></returns>
    public static Encoding DetectEncoding(String fileName, out String contents)
    {
        // open the file with the stream-reader:
        using (StreamReader reader = new StreamReader(fileName, true))
        {
            // read the contents of the file into a string
            contents = reader.ReadToEnd();

            // return the encoding.
            return reader.CurrentEncoding;
        }
    }

답변

늦게 늦어지는 또 다른 옵션은 죄송합니다.

http://www.architectshack.com/TextFileEncodingDetector.ashx

이 작은 C # 전용 클래스는 존재하는 경우 BOMS를 사용하고, 그렇지 않으면 가능한 유니 코드 인코딩을 자동 감지하려고 시도하며, 유니 코드 인코딩이 가능하지 않거나 가능성이없는 경우 대체됩니다.

위에서 언급 한 UTF8Checker가 비슷한 것을하는 것처럼 들리지만 범위가 약간 넓다고 생각합니다 .UTF8 대신 BOM이 누락 될 수있는 다른 가능한 유니 코드 인코딩 (UTF-16 LE 또는 BE)도 확인합니다.

이것이 누군가를 돕기를 바랍니다!


답변

SimpleHelpers.FileEncoding Nuget 패키지는 랩 모질라 범용 캐릭터 세트 감지기의 C #을 포트 죽은 – 간단한 API로를 :

var encoding = FileEncoding.DetectFileEncoding(txtFile);

답변

내 해결책은 몇 가지 대체 기능이있는 내장 기능을 사용하는 것입니다.

stackoverflow에 대한 다른 유사한 질문에 대한 답변에서 전략을 선택했지만 지금 찾을 수 없습니다.

StreamReader의 내장 로직을 사용하여 BOM을 먼저 확인합니다. BOM이 있으면 인코딩이 아닌 다른 인코딩이되므로 Encoding.Default그 결과를 신뢰해야합니다.

그렇지 않은 경우 바이트 시퀀스가 ​​유효한 UTF-8 시퀀스인지 확인합니다. 그렇다면 UTF-8을 인코딩으로 추측하고 그렇지 않으면 기본 ASCII 인코딩이 결과가됩니다.

static Encoding getEncoding(string path) {
    var stream = new FileStream(path, FileMode.Open);
    var reader = new StreamReader(stream, Encoding.Default, true);
    reader.Read();

    if (reader.CurrentEncoding != Encoding.Default) {
        reader.Close();
        return reader.CurrentEncoding;
    }

    stream.Position = 0;

    reader = new StreamReader(stream, new UTF8Encoding(false, true));
    try {
        reader.ReadToEnd();
        reader.Close();
        return Encoding.UTF8;
    }
    catch (Exception) {
        reader.Close();
        return Encoding.Default;
    }
}