FString contain Chinese character

in c++,I wrote the following code:

FString show()
{
FString a=“你好世界”;
return a;
}

in editor,it shows ???,what’s wrong?How to handle this problem?

Because FString doesn’t support Chinese…
Use FText instead. It can work!

Use L"你好世界" instead of “你好世界”. Also, make sure your files are saved in utf8 format.

This works:



FString UTestStructLib::GetTest_UnicodeString(){
	FString arg(L"猫又");
	return arg;
}


FString supports chinese text just fine (according to documentation strings are strored in UTF16 encoding internally), however, when you write “你好世界” as string literal, it indicates that it is char* string. So, the system first converts your chinese string to your local 8bit encoding, and then uses result to initialize FString, which results in data loss in OPs case.

So you should either ensure that your text files are in utf8 or use unicode string literals. ( L"你好世界").

If the string is not string literals,what can i do?
For example:

FString UTestStructLib::GetTest_UnicodeString(){
char* pszRecv = new char[4096];
std::string TestString(pszRecv);
FString arg(FString(TestString.c_str()));
return arg;
}

If you want chinese characters in your program, you shouldn’t use char, char* or std::string types.

Unless you know for sure that that char* string contains utf8 encoded text, you’ll run into trouble.

Also, do not allocate buffers with “new”, unless you really have to. Your example has memory leak, because you forgot to delete pszRecv.

Use FString, FName or FText types when working in unreal 4.

If you need to load string from file, this article explains how to do it:

char* pszRecv = new char[4096];
nRet = recvfrom(sockClient, pszRecv, 4096, 0, (SOCKADDR*)&siRemote, &(dwSendSize));
pszRecv[nRet] = ‘\0’;
std::string TestString(pszRecv);
FString HappyString(FString(TestString.c_str()));
pDlg->OnDataUpdated.Broadcast(HappyString);

I used socket to communicate with another program.The third parameter of the function ‘recvfrom()’ needs char *,so I must define a char * array.Once Ireceived data,I will send the data to my blueprint.
The problem is that the data i received contained Chinese character,and it shows uncorrectly in blueprint.I can not fix it.

According to documentation:

You’ll need to ensure that external program sends all strings in utf8 encoding, and then use UTF8_TO_TCHAR macro to load that data into FString. That is the best approach.

If that cannot be done, you’ll need to search engine source code for subroutines that can convert data from character encoding you receive to FString. Alternatively you could use OS-specific calls (not recommended) or 3rd party libraries to do the conversion.

This article is marked as recommended reading on wiki page, and most likely is worth reading: