Special characters are replaced with question marks when printed to disk

In the macro,
step 1: I get string S out of the InputBox(…)
step 2: Print S
step 3: Print #FileNum, S
If S contains special characters (CHR(191) and beyond, e.g. cyrillic) then step 3 changes these characters to question mark “?” (while step 2 doesn’t).
How to fix it?

LO: 6.3.6.2, Default language for documents: French (France), Windows: 10

PrintSym.ods (10.8 KB)

Please edit your sample file, and put those characters into a cell. Then we will able to try it in the Form (by copy-paste) even if we have not cirillic keyboard, and locale settings.

SbiRuntime::StepPRINTF uses SbiIoSystem::Write, which uses thread encoding (basically, system encoding) to encode the 8-byte string. Given that you use a French Windows, i.e. your system encoding is windows-1252, which cannot represent Cyrillic characters, the conversion replaces all non-representable characters with question marks. Also Put has the same result.

LibreOffice uses Unicode internally, so your first print command may render the unicode to screen/console and use the appropiate charset of the console.
.
Writing to a file via #FileNum may either not replacing anything, or respecting other settings. As windows comes from using codepages, this default may differ from console.
.
You may try to change the console to unicode with command chcp 65001
But as not every program for windows can handle unicode, I won’t use this as a general setting.

Console codepage has no effect on LibreOffice Basic Print to file operation. One could set language for non-Unicode programs to utf-8 (which is still beta on Windows), but be prepared for any kinds of problems then from software that isn’t prepared for such a setup.

The recipe is simple - don’t use Basic Print # (this is true for VBA as well).
Use SimpleFileAccess to read and write text files. See section “8.9.4 Streams” of A.Pitonyak’s classic book OOME_4_0.odt.

3 Likes

Many thanks to you and Pitonyak