String literal seems not work correctly

The string u"虚幻四真好用" hold different value in ue4 and normal vs project.

Above is the value in the normal vs project with vs2017, and the encoding format for this .cpp file is gbk.

Above is the value in the ue4 project with ue4 4.18.3, and the encoding format for this .cpp file is gbk.

(well, changing this file to utf-8 can make it correctly.

Is this a bug ?

This is not the way you are supposed to handle text in Unreal!

You are giving the control over to the compiler to decide what exactly do you mean by u"虚幻四真好用" in bytes. Unreal uses a custom compiler which means that some things are bound to be different than the normal Visual Studio.

I strongly suggest you use the built-in data types in unreal like FString and FText to deal with strings.

Check this for more information: String Handling | Unreal Engine Documentation

UE4 uses “wchar_t” by default to handle strings, which is different from compiler-generated UTF-16 strings (what you are trying to do) because they use “char16_t”. “wchar_t” has a different size depending on the platform (typically 2 bytes on Windows and 4 bytes on Unix-based systems), whereas "char16_t’ is always 2 bytes per character.

Instead of saying u"虚幻四真好用", you need to say TEXT(“虚幻四真好用”).

The implement of TEXT() is to add a prefix ‘L’ to the string, which in most case works the same as prefix ‘u’ in windows.
In fact, TEXT() still do not work correctly. The value in FString is not the same as the string literal.
You can test this on your own, and do not forget to change the encoding format of the .cpp file to gbk.

But prefix ‘u’ is one of the c++ standard, which makes sure that the value of the string is in utf16.
If ue4’s compiler does not support this standard, it should tag that as an error.
This cause a lot of trouble in building the three-part library in ue4.
By the way, FString does not work correctly, too. The only solution is to change the code file to utf8.
It seems that the encoding format of the string is depending on the encoding format of the code file.

Ok. I don’t know how to better explain it…

Unreal documentation has a whole section dedicated to using strings.

If you go out of your way to intentionally circumvent the engine, it’s memory management (with std::memcpy()) and data types (unit16_t and char16_t) it should no longer be held responsible for the outcome.

No error is tagged because no error has occurred. You copy some memory from one place to the other. The compiler does not care if you have taken the right memory or you interpret it in the right way. This is up to you.

I guess that the compiler do not take the code file as a gbk file.
Any way , thank you.

I guess the compiler do not take the code file as a gbk file.
Any way, thank you.