1

I'm using ncurses with noecho(), and I'm trying to print a string from a TCHAR (or char16_t) array with addch() function.

I've tried casting my TCHAR to an int, but with the same result.

This is the code I'm using:

coords hCursorPosition( GetCursorPosition() );
        if ( hCursorPosition.X == 0 ) return;
        coords nCursorPosition(hCursorPosition.Y, 0);
        SetCursorPosition(nCursorPosition);
        clrtoeol();
        if (!m_sInput.IsEmpty())
        {
            for (auto CharIt(m_sInput.CreateConstIterator()); CharIt; CharIt++)
            {
                const TCHAR Char = *CharIt;
                int intChar = static_cast<int>(Char);
                addch(intChar);
                refresh();
            }
        }

m_sInput is an FString (a type used in Unreal Engine 4), I've checked the FString length and it's correct, while the result isn't what I expect.

For example if m_sInput is "test", my output will be "test^@"

Scienziatogm
  • 53
  • 1
  • 6
  • You are on Windows, right? This might help: [Display wchar_t using ncurses](https://stackoverflow.com/questions/15222466/display-wchar-t-using-ncurses) – Ted Lyngmo Jul 03 '19 at 20:18

1 Answers1

3

addch expects a chtype parameter, which contains an 8-bit character (and if you happen to pass it a NUL, it will show that as ^@).

wchar_t holds more than an 8-bit character. Use addwstr for that.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Thank you for the answer! Anyway I made a mistake: TCHAR is a char16_t, so I'm getting this compile error when using addwstr: error : cannot initialize a parameter of type 'const char *' with an lvalue of type 'const TCHAR' (aka 'const char16_t') – Scienziatogm Jul 03 '19 at 20:38
  • Solved replacing the code with this: ```if (!m_sInput.IsEmpty()) { addstr(TCHAR_TO_ANSI(*m_sInput)); refresh(); }``` – Scienziatogm Jul 03 '19 at 21:04
  • @Scienziatogm `TCHAR` is whatever size you tell the compiler to make it. It is intended to make porting code from old DOS-based OSes to Windows NT-based based OSes. A 20-something year old solution to a problem from 20-something years ago. These days it's usually best if you avoid using `TCHAR` to prevent interesting surprises like this one. – user4581301 Jul 03 '19 at 21:53
  • @user4581301 `TCHAR` is defined by Unreal Engine to support a wide range of platforms, and from what I can see in practice it is always defined as `wchar_t`. – Rotem Jul 04 '19 at 02:25
  • @Rotem Yes, but in this case, it is defined as a const char16_t – Scienziatogm Jul 04 '19 at 12:50