Universal/portable way to print ansi/UTF8/unicode text file to console

New to FreeBASIC? Post your questions here.
Post Reply
PeterHu
Posts: 156
Joined: Jul 24, 2022 4:57

Universal/portable way to print ansi/UTF8/unicode text file to console

Post by PeterHu »

Greetings!

File name and content in ansi/UTF8/unicode format ,to print to console,(short text) to show in a messagebox.
Recently I searched in forums and libraries but unlucky failed to find an universal way to make the file content showing properly.Some even failed to read the file.chcp ,#define UNICODE... all tried.Just wondering people in other countries how to manage this issue(files in Russian,Japanese...)

Appreciated for the help.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Universal/portable way to print ansi/UTF8/unicode text file to console

Post by caseih »

That's a real can of worms! At least on Windows. The issues is this is not a FB matter at all. It's a console matter. The console has to interpret the bytes (decode). So that requires a manual step of setting the console's code page to UTF-8. On recent versions of Windows 10 and 11, the console defaults to UTF-8, so life is considerably better. And the new MS Terminal is UTF-8 by default as well.

I suppose a fall-back for older versions of Windows is to convert your UTF-8 bytes into UTF-16 bytes for printing to the console. See https://stackoverflow.com/questions/249 ... onsole-app . But just be aware the console traditionally was very limited in terms of font support for unicode. And in years past, languages were dealt with exclusively with code page and 8-bit bytes. You set your OS language and that was it.

On any other OS than Windows, just print the bytes to the console directly. UTF-8 is the standard on Mac, Linux, and most unix OSs. So you do nothing on these operating systems.
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: Universal/portable way to print ansi/UTF8/unicode text file to console

Post by angros47 »

Also, since the question is not in the Windows subforum, we must assume that the "universal way" is related to all FreeBasic version, not just windows.

So, I must add: under DOS, there is no way at all to print unicode text to the console, since the console supports only 256 characters, and not always the same, depending on the localization and the layout.

Older versions of Windows (9x, XP) basically emulate the DOS console, so they have the same limits (or even worse, since under DOS it's possible to redefine characters, with some effort, at least)

Under Linux, there are so many different terminals that it's almost impossible to answer: many terminal emulators offer support for Unicode, but not all of them do. In text only mode, if the framebuffer is used unicode is supported, while in VGA mode it's not.
PeterHu
Posts: 156
Joined: Jul 24, 2022 4:57

Re: Universal/portable way to print ansi/UTF8/unicode text file to console

Post by PeterHu »

Thank you all for the help!
Post Reply