Construct a FString from a std::string in UTF-8 encoding

Hello,

I have tried many things to do this conversion, reading Unreal documentation, searching in Source code of Engine… no way.

Here the link that explains how convert std::string to FString in Unreal Doc : Unreal Documentation
It works fine, but when I add special characters…


std::string TestString = "Hèappyé";
FString HappyString(TestString.c_str());

==> Value of HappyString: “H?appy?”

The only way I found to trick this problem is to use the TEXT() property, like this :


std::string TestString = "Hèappyé";
FString HappyString(TEXT(Hèappyé));
std::string MyStdStr(TCHAR_TO_UTF8(*HappyString));

==> Value of HappyString: “Hèappyé”
But… value of MyStdStr: “Hèappyé”.

It seems encoding is impossible in this case…
How fix my problem ?

Thanks in advance.

Up ! :slight_smile:
(still with the same problem)

Up. Up. Up…

A quick solution is



std::string TestString = "Hèappyé";
	FString OutputString = "";
	for (uint8 Char : TestString) {
		OutputString += Char;
	}


I’m pretty sure bumping your own post is dissuaded as well.
Good luck!

Any solution yet?

Incoming string must be utf8 then



FString HappyString(UTF8_TO_TCHAR (TestString.c_str()));


But if you really need localization just use FText instead.

2 Likes