Recent

Author Topic: Convert String to Unicode String ?  (Read 1492 times)

nhatdung

  • New Member
  • *
  • Posts: 37
Convert String to Unicode String ?
« on: June 03, 2021, 08:59:41 am »
Hi!

I have a service write in FPC running (no UI) under linux.
I got a problem when compare a string is "valid"

Quote
OS Debian 9, FPC version 3.2.2
here is the code
Code: Pascal  [Select][+][-]
  1. const valid_text2 = 'ơâABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';
  2. var s : string;
  3.     sw : UnicodeString;
  4.     i : integer;
  5. begin
  6.   s := 'Sơn tây';
  7.   sw := s;
  8.   for i := 1 to length(s) do
  9.     if pos(sw[i], valid_text2) <= 0 then
  10.       Writeln('index = ', i, ', value = ', ord(sw[i]), ' => ', ' not valid');
  11. end.
  12.  

result is
Code: Pascal  [Select][+][-]
  1. index = 2, value = 198 =>  not valid
  2. index = 3, value = 161 =>  not valid
  3. index = 7, value = 195 =>  not valid
  4. index = 8, value = 162 =>  not valid
  5.  

after looking i found out fpc convert my unicode string to utf8 string, that's why my code doesn't work anymore compare to Delphi in Windows.

In Windows char 2 = 417 and char index 6 = 226.

How i convert that unicode string with encoded by fpc in utf8 to REAL unicode UTF-16 ?



paweld

  • Sr. Member
  • ****
  • Posts: 289
Re: Convert String to Unicode String ?
« Reply #1 on: June 03, 2021, 09:31:32 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.   LazUTF8;
  3.  
  4. {...}
  5.  
  6. const valid_text2 = 'ơâABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';
  7. var
  8.   s : string;
  9.   i : integer;
  10. begin
  11.   s := 'Sơn tây';
  12.   for i := 1 to UTF8Length(s) do
  13.     if pos(UTF8Copy(s, i, 1), valid_text2) <= 0 then
  14.       Writeln('index = ', i, ', value = ', UTF8Copy(s, i, 1), ' => ', ' not valid');
  15. end.
Best regards
paweld

nhatdung

  • New Member
  • *
  • Posts: 37
Re: Convert String to Unicode String ?
« Reply #2 on: June 03, 2021, 10:56:56 am »
thanks!

work perfect!

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3937
  • I like bugs.
Re: Convert String to Unicode String ?
« Reply #3 on: June 03, 2021, 11:30:43 am »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

nhatdung

  • New Member
  • *
  • Posts: 37

 

TinyPortal © 2005-2018