Best way to convert between FString and std::string

Hi guys,

I am not the best with C++ so excuse my ignorance.

Does anybody have some code at hand to convert between FString and std::string (and the other way) effectively?

I tried using the UTF8_ functions but kept having errors/crashes. (obviously not using it correctly)

Many thanks in advance for this and I apologise if it has been asked before.

so do you mean you need to convert to ANSICHAR or what?

because, isn’t std::string typed and thus unicode on modern systems?
Then it should be as easy as writing MyStdString = std::string(*MyFString);
or the other way MyFString = FString(MyStdString.c_str()) ?

of you need to convert between TCHAR and ANSICHAR, take a look at the TStringConvert class

I’ve only been looking at it for about 4 minutes. Here’s what I found:

FString stores its data in a TArray<TCHAR> - Runtime/Core/Public/Containers/UnrealString.h at line 41.
TArray is defined in Runtime/Core/Public/Containers/Array.h.
TArray::GetData() and ::GetTypedData() return pointers to the first element in the data, but I’m not sure if it guarantees that all elements are contiguous and null-terminated as MyStdString = std::string(*MyFString) would expect.

In theory then, MyStdString = std::string(MyFString->GetTypedData()) might work…