Delpi er ikke særlig glad for UNI-code.
Jeg har på denne adresse
http://groups.google.dk/group/borland.public.delphi.winapi/browse_thread/thread/54c968374f709cf7/8fe74c87f73c5b91?lnk=st&q=read+unicode+file+*delphi*&rnum=8#8fe74c87f73c5b91fundet nedenstående kode, som virker.
Procedure SwapBytesInWideString( Var ws: WideString );
var
i: Integer;
begin
for i:= 1 to Length( ws ) do
//ws[i] := Swap( ws[i] );
// if compiler balks at this try
ws[i] := WideChar( Swap( word( ws[i] )));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ws : WideString;
fs : TFileStream;
begin
If OpenDialog1.Execute then
begin
fs:=TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
try
SetLength( ws, fs.size div 2 );
fs.ReadBuffer( ws[1], fs.Size );
If ws[1] = #$FFFE Then
SwapBytesInWideString( ws );
If ws[1] = #$FEFF Then
Delete( ws, 1, 1 );
RichEdit1.Text:= ws;
finally
fs.free
end;
end;
end;
Den tilhørende forklaring er:
It is the recommended way to start a unicode text file, and documented in
win32.hlp (topic "Byte-order Mark"). Its purpose is to handle UNICODE files
written on other platforms that use a different byte order (big-endian instead
of the Intel little-endian). If you read the first word of a UNICODE file and
see that it is $FFFE instead of $FEFF you know that you have to swap the bytes
in each word you read to get a valid Widechar for your platform. Delphi has a
Swap function that performs this byte order switch.
So if you read a UNICODE file you have to be prepared to deal with files that
have the correct byte order mark for your platform, that need to be swapped
and those that do not have a byte order mark at all (since it is recommended
but not enforcible, of course).