Search code examples
c++unreal-engine4

How to convert char* to TCHAR in Unreal Engine 4?


For example,

const char* bytes = "somemultibytecharacter一些宽字符";
size_t n = strlen(bytes);

How to convert bytes to FString or TCHAR* in Unreal Engine C++ code?

I know I can convert with std::mbstowcs or MultiByteToWideChar, but I'm trying to find a UE4 alternative.


Solution

  • Just use FString(int32 InCount, const CharType* InSrc).

    Usage:

    const char* bytes = "somemultibytecharacter一些宽字符";
    size_t n = strlen(bytes);
    const FString& Str = FString(n, bytes);
    const TCHAR* Text = *Str;
    

    Note, in my copy of Unreal Engine 4, TCHAR is wchar_t:

    typedef wchar_t  WIDECHAR;
    typedef WIDECHAR TCHAR