C/C++ Programming – Determine Terminal Character Encoding

asciicharacter encodingterminal-emulatorterminfounicode

I've noticed that SyncTERM uses a different character encoding than the default MacOS terminal emulator, and they're incompatible with one another. For example, say you want to print a block character in a format string. In SyncTERM, which uses the IBM Extended ASCII character encoding, you would use an octal escape sequence like \261. In Terminal.app (and probably iTerm2 as well), this just prints a question mark. Since these terminals use UTF-8, you need to use the \uxxxx escape sequence.

So let's say you want to print a certain, not-ASCII, character in a format string, and you want it to work in all terminal emulators, regardless of character set. I'm guessing you would use an entry in the terminfo database, but I'm not really familiar with terminfo. I need some pointers here.

Best Answer

Short:

  • terminfo won't take you there, won't help
  • there is no reliable way to determine what encoding a terminal actually uses
  • starting from Unicode literals is the way to go, provided that you know what encoding to want to use on the terminal
  • the user has to know what locale is appropriate and what encoding the terminal can do
  • the C standard has functions for converting "wide" characters which you will have available on any Unix-like platform (see for example setlocale, wcrtomb and wcsrtombs)