1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| const UTF32BE: array[0..3] of byte = (0,0,$FE,$FF);
UTF32LE: array[0..3] of byte = ($FF,$FE,0,0);
var
BOM: Int32;
S, Txt:string;
St:TFilestream;
R, Len: Integer;
Us:UCS4String;
Bins: TBytes;
begin
if not PromptForFileName(S) then
Exit;
St := TFilestream.Create(S, fmOpenReadWrite);
try
if St.Read(BOM, 4) = 4 then
begin
if BOM = Int32(UTF32BE) then
begin
Showmessage('UTF32 BE détecté - non converti');
end;
if BOM = Int32(UTF32LE) then
begin
Len := St.Size div SizeOf(UCS4Char) - 1;
SetLength(Us, Len);
St.Read(Us[0], Len * SizeOf(UCS4Char));
St.Size := 0;
Txt := UCS4StringToUnicodeString(Us);
Bins := [$EF, $BB, $BF];
St.WriteBuffer(Bins, 3);
Bins := TEncoding.UTF8.GetBytes(Txt);
St.WriteBuffer(Bins, Length(Bins));
Showmessage('UTF32 LE fichier converti');
end;
end;
finally
St.Free;
end;
end; |
Partager