How to set the encoding to output the Russian text in the TMemo?

The code works like this:
When I press the button, start the procedure procedure TfMain.ButtonEncryptClick(Sender: TObject), from field Memo1 to read the text and then handled in the main function encr_decr. At the end of the field Memo2 displays the encrypted text by a method of the magic square. So, the code works only with English characters, if to write in the Memo1 Russian text, the output in Memo2 get krakozyabry and question marks.

I have assumptions that this may be the fault of the function of the delete and the procedure swap. But rewriting the delete I don't get the expected result, and even errors started flying, eventually returned to the function delete. Instead of delete I wrote the code:
for num:=1 to UTF8Length(s)-n*n do
 sdel:= sdel + s[num+n*n];
s:= sdel;


Smoke forums for two days, but I still don't know how to solve the problem. You may want to re-write the procedure swapthat works with symbols, but there are no views what can change and should we even touch her.

unit main;

{$mode objfpc}{$H+}

interface

uses
 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, LazUTF8, LConvEncoding, StdCtrls;

type

 mas=array[1..10,1..10] of integer;

 { TfMain }

 TfMain = class(TForm)
 ButtonEncrypt: TButton;
 ButtonDecrypt: TButton;
 Memo1: TMemo;
 Memo2: TMemo;
 MemoMS: TMemo;
 ButtonDecryptClick procedure(Sender: TObject);
 ButtonEncryptClick procedure(Sender: TObject);
 procedure FormCreate(Sender: TObject);
private

public

end;

var
 fMain: TfMain;
A:mas;
i,j,n,k:integer;
lgthS:real;
s:string;

implementation

{$R *.lfm}

{ Programm }

kvadrat procedure(var A:mas;n:integer);
var
i,j:integer;
begin
 for i:= 1 to n do
 for j:= 1 to n do begin
 a[i,j]:= 1+((i+j-1+n div 2) mod n)*n+(i+2*(j-1)) mod n;
end;
end;

procedure swap(var c1,c2:char);
var
s2:char;
begin
 s2:= c1;
 c1:= c2;
 c2:= s2;
end;

function encr_decr(A:mas;stroka:string;enc_dec:boolean):string; // square,line,true-encrypt,false-decrypt
var
i,j,k,num:integer;
 l:array[1..100] of integer;
sc,s1,s,sdel:string;
s2:char;
begin
 s:= stroka;
 k:= 0;
 sdel:= ";
 for i:= 1 to n do
 for j:= 1 to n do
begin
 k:= k+1;
 l[k]:= A[i,j];
end;
 if n*n>Length(s) then while Length(s)<n*n do s:= s+' ';
 if Length(s) mod (n*n)>0 then while (Length(s) mod (n*n))>0 do s:= s+' ';
 sc:= ";
repeat
 s1:= Copy(s,1,n*n);
 if enc_dec then for i:= 1 to n*n do swap(s1[i],s1[l[i]])
 else for i:= n*n downto 1 do swap(s1[i],s1[l[i]]);
Delete(s,1,n*n);
 sc:= sc+s1;
until UTF8Length(s)<=1;
encr_decr:= sc;
end;

{ TfMain }

procedure TfMain.FormCreate(Sender: TObject);
begin
end;

procedure TfMain.ButtonEncryptClick(Sender: TObject);
var
 str: string;
begin
 s:= Memo1.Text;
 lgthS:= UTF8Length(s);
 lgthS:= sqrt(lgthS);
 n:= round(lgthS);
 if n= lgthS lgthS then:= trunc(lgthS)
 else lgthS:= trunc(lgthS)+1;
 if lgthS<3 then lgthS:= 3;
 if round(lgthS) and 1 = 0 then lgthS:= lgthS+1;
 n:= round(lgthS);

 kvadrat(A,n); // calculation of square
MemoMS.Clear;
 str:= ";
 for i:= 1 to n do begin
 for j:= 1 to n do begin
 str:= str + '' + IntToStr(A[i, j]);
end;
MemoMS.Lines.Add(str);
 str:= ";
end;

 Memo2.Text:= ( encr_decr(A,s,true) );
end;

procedure TfMain.ButtonDecryptClick(Sender: TObject);
begin
end;

end.
March 20th 20 at 11:54
1 answer
March 20th 20 at 11:56
Solution
Found the answer to your question. So the encoding was displayed as it should, you need all the data types string and char rename to widestring and widechar. Thus, each symbol will be allocated in two bytes instead of one.

Find more questions by tags PascalLazarus