Forum > General

concatenate two ansistring in console application

<< < (4/4)

kjteng:
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  [+][-]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";}};} ---program concat1;{$mode objfpc}{$H+} // {$Codepage UTF8}uses windows, sysutils; {$R *.res} var s3, s1, s2: AnsiString;  procedure test1;begin  writeln('Call SetTextCodePage and set Ansi Codepage to utf8 :-');  SetTextCodePage(output, cp_utf8);  s3 := s1+s2;  write('s1=', s1,'  s2=', s2, '  s3=', s3);end; procedure test2;begin  writeln('Use Strcat (no need to call SetTextCodePage) :-');  S3 := '';  strcat(pchar(s3), pchar(s1));  strcat(pchar(s3), pchar(s2));  writeln('pchar(s1)=', pchar(s1), '  pchar(s2)=', pchar(s2),        '  pchar(s3)=', pchar(s3));  writeln;end; procedure test3;begin  writeln('By casting to shortstring (no need to call SetTextCodePage) :-');  SetTextCodePage(output, cp_utf8);  S3 := shortstring(s1) +shortstring(s2);  writeln('pchar(s1)=', pchar(s1), '  pchar(s2)=', pchar(s2),          '  pchar(s3)=', pchar(s3));  writeln;end;  begin  SetConsoleOutputCP(CP_utf8);  s1 := '春夏'; //utf8Encode(u1);  s2 := '秋冬'; //utf8Encode(u2);  test2;  test3;  test1;  readln;end. 

       

Thaddy:
I did it like this. Also select font that is supported.
--- 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";}};} ---{$mode delphiunicode}{$H+}{$codepage utf8}uses windows, sysutils;var   s3, s1, s2: string;  var  cfi: CONSOLE_FONT_INFOEX;begin  cfi.cbSize := SizeOf(cfi);  cfi.nFont := 0;  cfi.dwFontSize.X := 0;  cfi.dwFontSize.Y := 20;  cfi.FontFamily := FF_DONTCARE;  cfi.FontWeight := FW_NORMAL;  // two fonts that support chinese etc.  cfi.Facename := 'NSimSun';  // cfi.FaceName := 'SimSun-extB'; // this is a raster font.  SetTextCodePage(output,cp_utf8);  setconsoleoutputcp(cp_utf8);  SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), False, @cfi);  s1 := '春夏';   s2 := '秋冬';   s3 := s1+s2;  writeln('s1=', s1,'  s2=', s2, '  s3=', s3);  readln;end.

edit: Ah, I see that Bart hinted at that.

kjteng:

--- Quote ---edit: Ah, I see that Bart hinted at that.

--- End quote ---
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.

AlexTP:
Wrote Thaddy's example to the wiki
https://wiki.freepascal.org/Console_Mode_Pascal#Output_CJK_text_in_Windows_console

Navigation

[0] Message Index

[*] Previous page

Go to full version