Hello! I can not understand how to work with encodings, or rather how I should be with them.

More ... There are 2 programs: 1 console application, the second simple program. I create a file with the program, encrypt the text and write it to a file. Through the console, I open this file, decipher the text and display it on the screen.

The problem is that this text is displayed in a different encoding. Using the "decoder" , I got the result I needed. It turns out that the text in the CP866 encoding is output to the console from the file, when the file itself, from where the text was taken, is in UTF-8 (without BOM) encoding.

I tried various options for changing the encoding. From a simple UTF8ToConsole (after the last release, Cyrillic is not displayed in the console when using this function, I personally changed the unit encoding just to CP866, otherwise I can’t display Russian characters to the console) to several functions that change the CP866 encoding → CP1251 → UTF -8, and so on. Whatever I do, nothing comes out. How can this be fixed?

PS I tried to change the encoding of the text when writing to the file. In my opinion, there is no need to dig, because text is encrypted before recording.

File

UPD1

readln(p); assignfile(f,p); reset(f); while not EOF(f) do begin Readln(f,s); writeln(AES.DecryptString(s)); writeln(s); end;

After all the attempts I decided to leave it as it is now. Encoding unit CP866.

  • one
    Please provide the code to read from the file and display the information in the console. Perhaps you are not using transcoding there. - kami
  • @kami, done. I can provide links from where I took and tested the transcoding functions. - GinTR1k

1 answer 1

 procedure Writeln2(const S: string); var s2: string; begin SetLength(s2, Length(S)); CharToOem(PChar(S), PChar(s2)); Writeln(s2); end; 

For some reason, foolishly, I did not try this function. The problem was solved using this and UTF8ToAnsi functions.

Those. the following turned out: writeln2(UTF8ToAnsi(AES.DecryptString(s))); .

Special thanks to kami for taking the time and solving the problem in the past topic .

  • @kami, so I still have the file in UTF8. Well, at least Notepad ++ writes like that. Is it really necessary then to use this code ? - GinTR1k 7:02
  • My mistake was not to see that your code is being used not for writing to a file, but for output to the console. Delete the previous comment. - kami
  • @kami, if you need to import and export any data, then use TFileStream ? - GinTR1k
  • SetConsoleOutputCP(65001); not easier? - Alekcvp