Forum > General

Convert String to Unicode String ?

(1/1)

nhatdung:
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
--- End quote ---
here is the code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const valid_text2 = 'ơâABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';var s : string;    sw : UnicodeString;    i : integer;begin  s := 'Sơn tây';  sw := s;  for i := 1 to length(s) do    if pos(sw[i], valid_text2) <= 0 then      Writeln('index = ', i, ', value = ', ord(sw[i]), ' => ', ' not valid');end. 
result is

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---index = 2, value = 198 =>  not validindex = 3, value = 161 =>  not validindex = 7, value = 195 =>  not validindex = 8, value = 162 =>  not valid 
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:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  LazUTF8; {...} const valid_text2 = 'ơâABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .';var  s : string;  i : integer;begin  s := 'Sơn tây';  for i := 1 to UTF8Length(s) do    if pos(UTF8Copy(s, i, 1), valid_text2) <= 0 then      Writeln('index = ', i, ', value = ', UTF8Copy(s, i, 1), ' => ', ' not valid');end.

nhatdung:
thanks!

work perfect!

JuhaManninen:
For more information:
 https://wiki.freepascal.org/Unicode_Support_in_Lazarus

nhatdung:

--- Quote from: JuhaManninen on June 03, 2021, 11:30:43 am ---For more information:
 https://wiki.freepascal.org/Unicode_Support_in_Lazarus

--- End quote ---

thx

Navigation

[0] Message Index

Go to full version