Forum > General

concatenate two ansistring in console application

(1/4) > >>

kjteng:
In the following program, I am able to display S1, S2 correctly but failed to display S3 (which is S1+S2).  However, the same works if it is a normal window GUI application. Hope someone can help to point out my mistake. (note: debugger watch window shows S3= ????, so I think there is no problem with writeln statement)


--- 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; uses windows;{$mode objfpc}{$H+} {$R *.res} var u1, u2: unicodestring;    s1, s2, s3: AnsiString;  begin  SetConsoleOutputCP(CP_utf8);  u1 := utf8Decode('春夏');  u2 := utf8Decode('秋冬');  s1 := utf8Encode(u1);  s2 := utf8Encode(u2);  writeln(pchar(s1), #13#10, pchar(s2));  s3 := s1 + s2;  // expect '春夏秋冬'' but got '????' in console apps  writeln(pchar(s3));  readln;end.    
Attached is the gui version (which display s3 correctly)

cdbc:
Hi
In your console app, you could try this:
--- 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+} { MODE COMES HERE }{$Codepage UTF8}   { make the sourcecode utf8 } uses windows;{ NOT HERE } {$R *.res} var u1, u2: unicodestring;    s1, s2, s3: AnsiString;  ... Mode changed place and the codepage modifier lets you use unicode literals in source...
Regards Benny

Thaddy:
Benny, it only makes the string literals unicode. Not the sourcecode.

kjteng:
thanks for the reply but that doesnot solve the problem. if i m not wrong, fpk source n string literal is by default utf8 encoded.
p/s i thought the string literal is by default utf8 encoded in this mode?

wp:
UTF8 in Windows console applications is still one of the greatest mysterious to me...

This is working (Win 11, Laz/main+FPC3.2.2): Add LazUtils to the project requirements and add LazUTF8 to the uses clause in order to get the UTF8 widestring manager. Then compile this 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";}};} ---program concat1;{$mode objfpc}{$H+}uses  LazUTF8;var  s1, s2, s3: AnsiString;begin  s1 := '春夏';  s2 := '秋冬';  writeln(s1, #13#10, s2);  s3 := s1 + s2;  writeln(s3);  readln;end;Depending on the codepage selected, the utf8 strings will not be displayed correctly in the IDE, but when you open a separate console window, type "chcp 65001" to change the codepage to utf8 and then run the application, it will be ok. SetConsoleOutput() has no effect on my system...

Navigation

[0] Message Index

[#] Next page

Go to full version