Search code examples
c++linuxncursesunreal-engine4

Getting an unexpected character with addch


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^@"


Solution

  • 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.