Sebastian Schon
Sebastian Schon

Reputation: 89

Convert std::string to FString (TCHAR / wstring) with special characters

Using Unreal Engine 4, I want to load a file from the machine, which contains characters like , , and .

All conversion attempts result in the final FString either containing ? in their place, or no character at all.

FString is UE4's internal string class, that uses TCHAR (wchar_t), which uses UTF-16 encoding.

Even desperate attempts to use this failed:

std::replace(str.begin(), str.end(), L'“', L'\"');

Nothing happened.

How can I properly convert between a std::string to an FString?

Upvotes: 2

Views: 10765

Answers (2)

Mohit Pundir
Mohit Pundir

Reputation: 107

You can convert std::string to FString and than log that like this.

std::string someString = "Hello!";

FString unrealString(someString .c_str());

UE_LOG(LogTemp, Warning, TEXT("%s"), *unrealString);

Than you can do the FString replace function on it. https://docs.unrealengine.com/en-US/API/Runtime/Core/Containers/FString/Replace/index.html

Upvotes: 0

Paradowski
Paradowski

Reputation: 719

Convert your std::string to std::wstring as it is also templated on wchar_t and try initializing your FString with it.
Check out this topic if you don't know how to convert to wstring: c++ can't convert string to wstring
Then you could do something like:
FString str = FString(your_wstring.c_str()); or
FString str(your_wstring.c_str());

You could also try to read data from file straight to wstring or even to FString because UE4 has its own classes for managing files like e.g. FFileHelper: http://api.unrealengine.com/INT/API/Runtime/Core/Misc/FFileHelper/index.html and I would really recommend you this last option :-)

Upvotes: 1

Related Questions