Recent

Author Topic: concatenate two ansistring in console application  (Read 2343 times)

kjteng

  • Sr. Member
  • ****
  • Posts: 262
Re: concatenate two ansistring in console application
« Reply #15 on: March 04, 2024, 05:37:05 am »
I. Many thanks to all of your replies and inputs.
In summary, we have to
1. call the followig two functions
        SetConsoleOutputCP(CP_utf8);
        SetTextCodePage(output, cp_utf8);
2. Go to project -> options -> Application settings (in Lazarus IDE) and tick Ansi codepage is utf8.


II. I have also tried some other ways and interstingly managed to circumvent the issue by (i) convert s1 and s2 to shortstring before concatenate them (this method has limitation of 255 chars for shortstring); (ii) concat s1 and s2 by StrCat method. Both (i) and (ii) does not call SetTextCodepage and does not change the ansi codepage setting. I am not sure this will cause any other problem.

Append below are the codes for your comment and correction. TQ.
Code: Pascal  [Select][+][-]
  1. program concat1;
  2. {$mode objfpc}{$H+}
  3.  
  4. // {$Codepage UTF8}
  5. uses windows, sysutils;
  6.  
  7. {$R *.res}
  8.  
  9. var s3, s1, s2: AnsiString;
  10.  
  11.  
  12. procedure test1;
  13. begin
  14.   writeln('Call SetTextCodePage and set Ansi Codepage to utf8 :-');
  15.   SetTextCodePage(output, cp_utf8);
  16.   s3 := s1+s2;
  17.   write('s1=', s1,'  s2=', s2, '  s3=', s3);
  18. end;
  19.  
  20. procedure test2;
  21. begin
  22.   writeln('Use Strcat (no need to call SetTextCodePage) :-');
  23.   S3 := '';
  24.   strcat(pchar(s3), pchar(s1));
  25.   strcat(pchar(s3), pchar(s2));
  26.   writeln('pchar(s1)=', pchar(s1), '  pchar(s2)=', pchar(s2),
  27.         '  pchar(s3)=', pchar(s3));
  28.   writeln;
  29. end;
  30.  
  31. procedure test3;
  32. begin
  33.   writeln('By casting to shortstring (no need to call SetTextCodePage) :-');
  34.   SetTextCodePage(output, cp_utf8);
  35.   S3 := shortstring(s1) +shortstring(s2);
  36.   writeln('pchar(s1)=', pchar(s1), '  pchar(s2)=', pchar(s2),
  37.           '  pchar(s3)=', pchar(s3));
  38.   writeln;
  39. end;
  40.  
  41.  
  42. begin
  43.   SetConsoleOutputCP(CP_utf8);
  44.   s1 := '春夏'; //utf8Encode(u1);
  45.   s2 := '秋冬'; //utf8Encode(u2);
  46.   test2;
  47.   test3;
  48.   test1;
  49.   readln;
  50. end.
  51.  


       
« Last Edit: March 04, 2024, 05:40:24 am by kjteng »

Thaddy

  • Hero Member
  • *****
  • Posts: 16422
  • Censorship about opinions does not belong here.
Re: concatenate two ansistring in console application
« Reply #16 on: March 04, 2024, 11:03:02 am »
I did it like this. Also select font that is supported.
Code: Pascal  [Select][+][-]
  1. {$mode delphiunicode}{$H+}{$codepage utf8}
  2. uses windows, sysutils;
  3. var
  4.   s3, s1, s2: string;
  5.   var
  6.   cfi: CONSOLE_FONT_INFOEX;
  7. begin
  8.   cfi.cbSize := SizeOf(cfi);
  9.   cfi.nFont := 0;
  10.   cfi.dwFontSize.X := 0;
  11.   cfi.dwFontSize.Y := 20;
  12.   cfi.FontFamily := FF_DONTCARE;
  13.   cfi.FontWeight := FW_NORMAL;
  14.   // two fonts that support chinese etc.
  15.   cfi.Facename := 'NSimSun';
  16.   // cfi.FaceName := 'SimSun-extB'; // this is a raster font.
  17.   SetTextCodePage(output,cp_utf8);
  18.   setconsoleoutputcp(cp_utf8);
  19.   SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), False, @cfi);
  20.   s1 := '春夏';
  21.   s2 := '秋冬';
  22.   s3 := s1+s2;
  23.   writeln('s1=', s1,'  s2=', s2, '  s3=', s3);
  24.   readln;
  25. end.


edit: Ah, I see that Bart hinted at that.
« Last Edit: March 04, 2024, 11:07:43 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

kjteng

  • Sr. Member
  • ****
  • Posts: 262
Re: concatenate two ansistring in console application
« Reply #17 on: March 04, 2024, 11:37:26 pm »
Quote
edit: Ah, I see that Bart hinted at that.
1. yeah. similar but i personally prefer your version because you made use of the declaration in windows unit rather than direct import from kernel32.dll.
2. I tried to change the font in console, but the system keep display with the font specified in the default profile. how can i override or disable the default command prompt settings?
OK I found the answer: (i) I have to run lazarus with administator rights and (ii) when not in IDE, run the apps in command prompt (as admin)
3. actually the main issue i have here is s3  := s1+s2 as s3 may become ???? if some of the setting is not set eg ascii codepage. with the help from all the experts here, this has been solved, but i just want to explore further.
« Last Edit: March 05, 2024, 03:54:35 am by kjteng »

AlexTP

  • Hero Member
  • *****
  • Posts: 2521
    • UVviewsoft

 

TinyPortal © 2005-2018