Recent

Author Topic: Is this right?  (Read 2542 times)

egsuh

  • Hero Member
  • *****
  • Posts: 924
Is this right?
« on: December 01, 2021, 08:28:37 am »
Hello,

I'm trying Korean characters. I tested something and found following result. 

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnTestClick(Sender: TObject);
  2. var
  3.    s: string;
  4.    ws: WideString;
  5. begin
  6.    s:= '가나다라 abc';               //  Each Korean characters seems to take 3 bytes
  7.    memo1.lines.add(IntToStr(Length(s)));        //   so, this gives 16 --- correct
  8.    ws:= s;                                       // I convert them to WideString
  9.    memo1.lines.add(IntToStr(Length(ws)));     //   8,  which is also correct
  10.    memo1.lines.add(copy(ws, 1, 2));              //  returns   '가나', which is correct
  11.    memo1.lines.add(copy(ws, 1, 6));              //  returns '가나다라 a', correct  
  12.  
  13.  
  14.    ws:= '가나다라 abc';                     // But if I directly assign to widestring,
  15.    memo1.lines.add(IntToStr(Length(ws)));     // this is 16, which means it is treated as Ansistring
  16.    memo1.lines.add(copy(ws, 1, 3));             // this gives wrong result. does not return '가'. Broken codces.
  17. end;


So, once I assign a string to normal string type variable and then re-assign it to widestring variable, it's OK. But assigning a string directly to Widestring type variable treats it as string. 

Is this correct operation or kind of bug?

malcome

  • Jr. Member
  • **
  • Posts: 78
Re: Is this right?
« Reply #1 on: December 01, 2021, 08:42:10 am »
You should explicitly use `UTF8Encode()` and `UTF8Decode()`.
Auto convert codepage often makes fail result.

 

TinyPortal © 2005-2018