Page 1 of 1

Loading Unicode string in Delphi XE

Posted: Sun Sep 18, 2011 6:08 pm
by marekjed
I apologize for the beginner's question. I've read "How to make plain text editor" and "How to make Unicode editor" in the Examples forum.

Using RichViewEdit in plain text mode, I'm using LoadTextFromStreamW to load a string (UnicodeString) into the editor. The result is garbled text (e.g. Chinese characters for a simple a-z string), and sometimes (depending on contents of the string) an exception: "Can't perform Unicode operation".

If I use LoadTextFromStream (no -W), the result is fine for text that "fits" in current codepage, but in other cases (Greek, Russian, Chinese) the text is loaded as question marks (typical result of processing Unicode text with ANSI methods).

All the settings described in "How to make Unicode editor" are correct (text styles set to Unicode, etc.)

The code I use is as follows:

Code: Select all

procedure TEditForm.SetAsText(const Value: string);
var
  ss : TStringStream;
begin
  ss := TStringStream.Create( Value );
  try
    ss.Position := 0;
    RVEditor.Clear;
    RVEditor.LoadTextFromStreamW( ss, 0, 0, True );
    RVEditor.Format;
  finally
    ss.Free;
  end;
end;

Posted: Sun Sep 18, 2011 6:14 pm
by marekjed
Solved: TStringStream must be created with TEncoding.Unicode as second parameter to the constructot, otherwise an ANSI stream gets created.