Ich habe mich an der ganzen Sache nun mal versucht. Schöner bekomme ich es leider nicht hin
Delphi-Quellcode:
function ByteToHex(AByte: Byte): string;
const
Digits: array [0 .. 15] of char = '0123456789ABCDEF';
begin
Result := Digits[AByte shr 4] + Digits[AByte and $0F];
end;
function BytesToHex(ABytes: TBytes): string;
var
i: Integer;
begin
for i := 0 to High(ABytes) do
Result := Result + ByteToHex(ABytes[i]);
end;
function GetFileEncoding(const FileName: string): string;
var
Stream: TBytesStream;
Bytes: TBytes;
begin
Stream := TBytesStream.Create;
try
Stream.LoadFromFile(FileName);
if Stream.Size >= 3 then
begin
SetLength(Bytes, 3);
Stream.ReadData(Bytes, 3);
end
else if Stream.Size >= 2 then
begin
SetLength(Bytes, 2);
Stream.ReadData(Bytes, 2);
end;
if BytesToHex(Bytes) = 'EFBBBF' then
ShowMessage('UTF-8 BOM')
else if BytesToHex(Bytes) = 'FEFF' then
ShowMessage('UTF-16 BE');
finally
Stream.Free;
end;
end;
Ich hätte auch noch das hier im Angebot:
Delphi-Quellcode:
function IsTextUnicode(const Text: string): Boolean;
var
C: Char;
begin
Result := False;
for C in Text do
begin
if C > #127 then
begin
Result := True;
Break;
end;
end;
end;
function IsFileUnicode(const AFile: string): Boolean;
var
i: Int64;
Stream: TBytesStream;
begin
Result := False;
Stream := TBytesStream.Create;
try
Stream.LoadFromFile(AFile);
for i := 0 to Stream.Size - 1 do
begin
if Char(Stream.Bytes[i]) > #127 then
begin
Result := True;
Break;
end;
end;
finally
Stream.Free;
end;
end;