2

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    First, you need to decide what encoding the `std::string` uses. Does it use UTF-8, or a system-defined locale? You can't convert it without answering that first. But when you do convert, you can use portable STL features like `std::wstring_convert()` or `mbsrtowcs()`, or Unicode libraries like ICONV or ICU, or platform-specific functions like `MultiByteToWideChar()` on Windows, etc. – Remy Lebeau Jun 13 '18 at 19:14
  • 1
    Thanks for your response. As it may have become obvious, I am not knowledgeable about all this, so my answers may sound stupid. We want to let user read any sort of file, so it would be nice if we could work with any (common) encoding. But I assume you mean the encoding of the string itself? When I did my research, it said that string does not use any particular encoding on it's own, just stores the bytes. How would I know which encoding it uses? – Sebastian Schon Jun 15 '18 at 12:47
  • that is for you to decide when you populate the string. When you are reading files, there is no common encoding. You have to know the encoding of each file. If you don't know, you are out of luck, unless you ask the user, or use hieristics to guess. – Remy Lebeau Jun 15 '18 at 14:47

2 Answers2

1

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 :-)

Paradowski
  • 719
  • 1
  • 13
  • 17
  • Hey, thanks for the response, I tried that already, wstring just completely discarded the characters not recognized by FString alltogether. No special '?', just not there anymore. In another conversion method, I at least now recognized a the - char, but nothing else. – Sebastian Schon Jun 30 '18 at 00:20
0

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

Mohit Pundir
  • 109
  • 1
  • 7