스트림에서 읽는 동안 BOM 바이트를 건너 뛸 수 있습니다. 추가 인코딩을 포함하도록 Bom.cs를 확장해야하지만 afaik UTF 만 BOM을 사용하는 인코딩입니다 ... (가장 가능성 있음) 잘못 생각할 수도 있습니다. 리틀 엔디안 또는,
나는 데이터 (또는 바이트 순서 마크없이)이나 또는 BOM withour UTF16 (중 UTF-8이 될 수
here
using (var stream = File.OpenRead("path_to_file"))
{
stream.Position = Bom.GetCursor(stream);
}
public static class Bom
{
public static int GetCursor(Stream stream)
{
// UTF-32, big-endian
if (IsMatch(stream, new byte[] {0x00, 0x00, 0xFE, 0xFF}))
return 4;
// UTF-32, little-endian
if (IsMatch(stream, new byte[] { 0xFF, 0xFE, 0x00, 0x00 }))
return 4;
// UTF-16, big-endian
if (IsMatch(stream, new byte[] { 0xFE, 0xFF }))
return 2;
// UTF-16, little-endian
if (IsMatch(stream, new byte[] { 0xFF, 0xFE }))
return 2;
// UTF-8
if (IsMatch(stream, new byte[] { 0xEF, 0xBB, 0xBF }))
return 3;
return 0;
}
private static bool IsMatch(Stream stream, byte[] match)
{
stream.Position = 0;
var buffer = new byte[match.Length];
stream.Read(buffer, 0, buffer.Length);
return !buffer.Where((t, i) => t != match[i]).Any();
}
}
에서 인코딩 유형에 대한 정보를 가지고 빅 엔디안)? –
제목을 편집했습니다. "[제목에"태그 "가 포함되어야합니까?] (http : // meta.stackexchange.com/questions/19190/) ", 공감대는"아니오, 그들은해서는 안됩니다. " –