Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-07

WinCE and strings

9 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Antonio Fortuny

Hi All.

Assuming that I'm modifying an application which
- will run on Win32 and WinCE (handheld device) (X-compilation, 2 targets)
- all string variables have previously been defined as String;
- WinCE (5.?) is UNICODE

Just to make sure that I'm on the right way, do you see someting wrong in the following sentences:

- all components using text (TLabel, TButton, TEdit, etc) use WideStrings
- I need to convert (using conditional code) from string to WideString when compiling for WinCE, like

Example:
function ShowSomething(const Msg: string): Boolean; <<<--------
unchanged function definition
var
{$IFDEF WINCE}
   wStr: WideString;
{$ELSE}
   wStr: String;
{$ENDIF}
begin
   result := False;
{$IFDEF WINCE}
   wStr := UnicodeString(msg);
{$ELSE}
   wStr := msg;
{$ENDIF}
   ALabel.Caption := wStr;
// I  know, the Caption could be assigned immediately instead of passing
through a variable
// this is just an example
...
   Result := True
end;

Thanks for your remarks

Antonio.

Reply by email

Sven Barth
to Antonio Fortuny
Antonio Fortuny wrote 11 lines
Hi All. Assuming that I'm modifying an application which - will run on Win32 and WinCE (handheld device) (X-compilation, 2 targets) - all string variables have previously been defined as String; - WinCE (5.?) is UNICODE Just to make sure that I'm on the right way, do you see someting wrong in the following sentences: - all components using text (TLabel, TButton, TEdit, etc) use WideStrings

Wrong. All components [provided by the LCL] use "AnsiString" with UTF-8 encoding on every platform/widgetset. Please see also http://wiki.lazarus.freepascal.org/LCL_Unicode_Support

- I need to convert (using conditional code) from string to WideString when compiling for WinCE, like

You only need to convert if you have text that is not encoded in UTF-8. Then you should use UTF8ToSys/SysToUTF8 or AnsiToUTF8/UTF8ToAnsi.

Show quoted text 26 lines
Example: function ShowSomething(const Msg: string): Boolean; <<<-------- unchanged function definition var {$IFDEF WINCE} wStr: WideString; {$ELSE} wStr: String; {$ENDIF} begin result := False; {$IFDEF WINCE} wStr := UnicodeString(msg); {$ELSE} wStr := msg; {$ENDIF} ALabel.Caption := wStr; // I know, the Caption could be assigned immediately instead of passing through a variable // this is just an example ... Result := True end; Thanks for your remarks
function ShowSomething(const Msg: String): Boolean;
begin
   result := False;
   ALabel.Caption := AnsiToUTF8(Msg); // if Msg is encoded in Ansi
   ALabel.Caption := Msg; // if Msg is already UTF-8 encoded
   ...
   Result := True;
end;

Regards,
Sven

Reply by email

Felipe Monteiro de Carvalho
to Sven Barth
Sven Barth wrote 5 lines
> - I need to convert (using conditional code) from string to WideString > when compiling for WinCE, like You only need to convert if you have text that is not encoded in UTF-8. Then you should use UTF8ToSys/SysToUTF8 or AnsiToUTF8/UTF8ToAnsi.

Between widestring and LCL strings a good pair to use is UTF8ToUTF16/UTF16ToUTF8

Felipe Monteiro de Carvalho

Reply by email

Sven Barth
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 9 lines
On Tue, Jul 10, 2012 at 9:58 AM, Sven Barth wrote: >> - I need to convert (using conditional code) from string to WideString >> when compiling for WinCE, like > > You only need to convert if you have text that is not encoded in UTF-8. Then > you should use UTF8ToSys/SysToUTF8 or AnsiToUTF8/UTF8ToAnsi. Between widestring and LCL strings a good pair to use is UTF8ToUTF16/UTF16ToUTF8

But his input parameter was String, not WideString. He only talked about WideString, because WinCE is based on WideString (or UTF-16) and thus concluded that the LCL must work the same way.

Regards,
Sven

Reply by email

Antonio Fortuny
to Sven Barth

Le 10/07/2012 11:29, Sven Barth a ?crit :

Show quoted text 17 lines
Am 10.07.2012 10:14, schrieb Felipe Monteiro de Carvalho: > On Tue, Jul 10, 2012 at 9:58 AM, Sven > Barth wrote: >>> - I need to convert (using conditional code) from string to WideString >>> when compiling for WinCE, like >> >> You only need to convert if you have text that is not encoded in >> UTF-8. Then >> you should use UTF8ToSys/SysToUTF8 or AnsiToUTF8/UTF8ToAnsi. > > Between widestring and LCL strings a good pair to use is > UTF8ToUTF16/UTF16ToUTF8 > But his input parameter was String, not WideString. He only talked about WideString, because WinCE is based on WideString (or UTF-16) and thus concluded that the LCL must work the same way.

This basically my way of thinking. My goal is to minimize as much as possible the modification affecting the main processing code and concentrate on the final display issues

Show quoted text 10 lines
Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Antonio Fortuny
to Antonio Fortuny: Hi All. Assuming that I'm modifying an application which - will run on Win32 and WinCE…

I have made a little test. One function

LabelInfo is a TLabel.

procedure TfMenu.BtnTestClick(Sender: TObject);
var
   Msg1: string;
   Msg2: AnsiString;
   Msg2a: AnsiString;
   Msg3: WideString;
begin
   Msg1 := 'Texte message'+#13#10+'???io?';
   LabelInfo.Caption := Msg1;
   MessageDlg('String', Msg1, mtError, [mbOK], 0, mbOK);
   Msg2 := SysToUTF8('Texte message'+#13#10+'???io?');
   LabelInfo.Caption := Msg2;          <-------------------------- shows
funny chars
   MessageDlg('UTF8',Msg2, mtError, [mbOK], 0, mbOK);
<-------------------------- shows funny chars
   Msg2a := AnsiToUtf8('Texte message'+#13#10+'???io?');
   LabelInfo.Caption := Msg2a; <-------------------------- shows funny chars
   MessageDlg('UTF8a',Msg2a, mtError, [mbOK], 0, mbOK);
<-------------------------- shows funny chars
   {$IFDEF WINCE}
   Msg3 := UnicodeString('Texte message'+#13#10+'???io?');
   LabelInfo.Caption := Msg3;
   MessageDlg('Unicode',Msg3, mtError, [mbOK], 0, mbOK);
   {$ENDIF}
end;

When using UTF8 conversion, displayed texts are rubbish. All other display OK on two lines. These results leave me a little bit confused Same results (rubbish with UTF8, WideString not generated) when compiled and run on Win32

Antonio.

Reply by email

Ludo Brands
to Antonio Fortuny
Show quoted text 5 lines
When using UTF8 conversion, displayed texts are rubbish. All other display OK on two lines. These results leave me a little bit confused Same results (rubbish with UTF8, WideString not generated) when compiled and run on Win32

If you create the file in Lazarus the string constants are already utf8.

Ludo

Reply by email

Felipe Monteiro de Carvalho
to Antonio Fortuny: I have made a little test. One function LabelInfo is a TLabel. procedure…

Well, look here:

   Msg2 := SysToUTF8('Texte message'+#13#10+'???io?');

You have text in UTF-8, and then you use the routine SysToUTF8 to convert it from system encoding to utf-8, which makes no sense. It should be like this:

   Msg2 := 'Texte message'+#13#10+'???io?';

Without any conversion. The same in the line with AnsiToUTF8. Now about the unicode, there are also errors:

   Msg3 := UnicodeString('Texte message'+#13#10+'???io?');
   LabelInfo.Caption := Msg3;
   MessageDlg('Unicode',Msg3, mtError, [mbOK], 0, mbOK);

You have a UTF-8 string and then you make a cast to UnicodeString. Unless you install a Lazarus widestring manager, FPC will make an automatic conversion system encoding to UTF-16. Which is wrong. The best is to make everything explicit, without relying in those hidden conversions, like this:

   Msg3 := UTF8ToUTF16('Texte message'+#13#10+'???io?');
   LabelInfo.Caption := UTF16ToUTF8(Msg3);
   MessageDlg('Unicode', UTF16ToUTF8(Msg3), mtError, [mbOK], 0, mbOK);

Felipe

Reply by email

Antonio Fortuny
to Antonio Fortuny: I have made a little test. One function LabelInfo is a TLabel. procedure…

OK guys.

I guess Ill need less Acetylsalycilic acid (aspirin) as initially projected. The trick is that Lazarus saves the string constants as UTF8 strings (as the files show) which means that the extra conversion is not needed and also explains why the simple string works very well along with the Unicodestring which converts UTF8-Unicode

I apologize for the misunderstanding.

Thanks to all.

Antonio.

Reply by email