Eine Funktion, die durch Auswertung der Byte-Order, die passende Unicode-Encoding ermittelt.
using System;
public class EncodingHelper
{
public static System.Text.Encoding DetectUnicodeEncoding(
System.IO.Stream s)
{
if (s.CanSeek == false)
throw new InvalidOperationException();
/* EF BB BF UTF-8
FF FE UTF-16, little endian
FE FF UTF-16, big endian
FF FE 00 00 UTF-32, little endian
00 00 FE FF UTF-32, big-endian */
byte[] byteOrder = new byte[4];
s.Read(byteOrder, 0, byteOrder.Length);
s.Seek(0, System.IO.SeekOrigin.Begin);
if ((byteOrder[0] == 0xEF) && (byteOrder[1] == 0xBB) && (byteOrder[2] == 0xBF))
return System.Text.Encoding.UTF8;
else if ((byteOrder[0] == 0xFF) && (byteOrder[1] == 0xFE))
return System.Text.Encoding.Unicode;
else if ((byteOrder[0] == 0xFE) && (byteOrder[1] == 0xFF))
throw new NotSupportedException();
else if ((byteOrder[0] == 0xFF) && (byteOrder[1] == 0xFE) && (byteOrder[2] == 0x0) && (byteOrder[3] == 0x0))
return System.Text.Encoding.UTF32;
else if ((byteOrder[0] == 0x0) && (byteOrder[1] == 0x0) && (byteOrder[2] == 0xFE) && (byteOrder[3] == 0xFF))
throw new NotSupportedException();
return null;
}
}
Kommentare zum Snippet