function ReadFile(FileName: string): string;var MyString: AnsiString; stream: TFileStream; StringLenth: Integer;begin StringLenth := FileSize(FileName); stream := TFileStream.Create(UTF8ToSys(FileName), fmShareDenyNone ); try stream.Seek(0, soFromBeginning); SetLength(MyString, StringLenth); stream.Read(MyString[1], StringLenth); finally stream.Free(); end; form1.Memo1.lines.Text := ConvertEncoding (MyString,'ansi', 'cp1200');end;
procedure TForm1.btnOpenClick(Sender: TObject);var nullword:word = 0;begin if OpenDialog1.Execute then begin if not cbForceType.Checked then fCES.Reset; fCES.LoadFromFile(OpenDialog1.FileName); fCES.Seek(0,soEnd); fCES.WriteBuffer(Nullword,sizeof(nullword)); Memo1.text := fCES.UTF8Text; cbBOM.Checked := fCES.HasBOM; cbForceType.Checked := fCES.ForceType; UniEnc.ItemIndex := Ord(fCES.UniStreamType); ANSIEnc.Enabled := fCES.UniStreamType = ufANSI; if ANSIEnc.Enabled then ANSIEnc.ItemIndex := ANSIEnc.Items.IndexOf(fCES.ANSIEnc) else ANSIEnc.ItemIndex := 0; end;
fCES.Seek(0,soEnd); fCES.WriteBuffer(Nullword,sizeof(nullword));
var nullword:word = 0;
Did you try if this solution works for you?
It's not NullStr, it's NullWord.And it's defined at the beginning of the procedure I posted.Code: [Select]var nullword:word = 0;
IOW: I'm adding two extra 0 bytes to the end of the stream to prevent the WideSwapEndian from overflowing.
fCES.WriteBuffer(NullWord,sizeof(NullWord)); fCES.WriteBuffer(NullWord,sizeof(NullWord));
var nullword:Dword = 0;
QuoteDid you try if this solution works for you?Yes..
Do you mean that I should either
Anyway, wont' this Nullwords result in an extra string, which I should remove?
procedure WideSwapEndian(PWC: PWideChar;size:integer);begin while size >= sizeof(widechar) do begin PWC^ := WideChar(SwapEndian(Word(PWC^))); inc(PWC); dec(size,sizeof(widechar)); end;end;
function TUniStream.GetUTF8Text: AnsiString;........ if fUniStreamType = ufUtf16be then WideSwapEndian(PWC,size);
procedure TUniStream.SetUTF8Text(AString: AnsiString); ....... if fUniStreamType = ufUtf16be then WideSwapEndian(@WS[1],size);
including without limitationthe rights to use, copy, modify, merge, publish, distribute, sublicense,