Convert std::string to FString

Is there a straightforward way to convert a std::string into an FString?

You should be able to do it with something like:

UTF8_TO_TCHAR(str.c_str())

There are three other important conversion functions available in StringConv.h

Be aware that this conversion is ā€œshort livedā€ and it is suggested that they only be used as function parameters.

1 Like

To add more to the discussion:

I had no trouble just doing this:



#include <string>

//....

some function
{
  std::string TestString = "Happy"; 
  FString HappyString(TestString.c_str());
}


You might want to check out UnrealString.h to see exactly how this is occurring :slight_smile:

Rama

PS: Bob_Gneu I really like your quote in your signature :heart:

7 Likes

Thanks guys!

Rama, your solution worked perfectly and it was so simple :). I’ll definitely take a look at UnrealString.h

I recently had to use TCHAR_TO_UTF8,

so I wanted to take a moment to thank Bob Gneu for mentioning StringConv.h

Thanks Bob Gneu!

:slight_smile:

Rama

How could it be done in reverse? Convert FString to std::string?

It can.

Check out **StringConv.h **it has a number of methods that swap back and forth between the types.

// Usage of these should be replaced with StringCasts.
#define TCHAR_TO_ANSI(str) (ANSICHAR*)StringCast<ANSICHAR>(static_cast<const TCHAR*>(str)).Get()
#define ANSI_TO_TCHAR(str) (TCHAR*)StringCast<TCHAR>(static_cast<const ANSICHAR*>(str)).Get()
#define TCHAR_TO_UTF8(str) (ANSICHAR*)FTCHARToUTF8((const TCHAR*)str).Get()
#define UTF8_TO_TCHAR(str) (TCHAR*)FUTF8ToTCHAR((const ANSICHAR*)str).Get()

Full Two Way Code

Bob is correct of course :slight_smile:

I just wrote up this code for you if you need exact code to work with!



FString UE4Str = "UE4 C++"; 

**//FString to std::string**
std::string cstr(TCHAR_TO_UTF8(*UE4Str));

**//std::string to FString**
ClientMessage(FString(cstr.c_str()));


Recall that operator* for FString gets you the inner data, which is TCHAR*

Enjoy!

Rama

PS: to run this code you need to #include <string>

3 Likes

char*

Hi Rama (and everybody)!

I must admit I’ve been lurking in the forums for quite some time now, but I never dared to post. Now, however I have a matter that needs solving:

What if I need to convert to char* ? I’m currently doing this, but it doesn’t strike me as nice code:


FString hlString = "Whatever";
std::string mlString(TCHAR_TO_UTF8(*hlString));
char* llString = &mlString[0];

Somehow the back of my brain tingles saying ā€œmemory managementā€ with the last line, but this is the best approximation of what I want based on multiple StackOverflow answers (except maybe the vector approach, but I do need a raw char*). Is there any way I could improve this? Also, is it okay to use TCHAR_TO_ANSI if I can guarantee only ANSI chars will be used? Is there any benefit in doing so?

As far as I can imagine, the inverse case is much safer, considering I’ve already managed the char*, isn’t it?


char* llString = "Something";
std::string mlString(llString);
FString hlString(FString(mlString.c_str()));

P.S.: Thanks for EVERYTHING you post, it’s a source of inspiration and it is always helpful.

Hi there!

[FONT=Comic Sans MS]**Welcome to the forums! **

You should post more often, you are a very eloquent writer!

You’re welcome!

:slight_smile:


Regarding your q:

This was the best answer I saw, for writable char * see the thread itself;
http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char

for **readonly** char* you can use this:



```


FString UE4Str = "UE4 C++"; 

//FString to std::string
std::string Your_Cstr(TCHAR_TO_UTF8(*UE4Str)):

//char*
const char * ReadOnlyCharPtr = Your_Cstr.c_str();


```



Rama

Wow! Thanks for the kind words and for answering so quickly. I’m posting the two resulting utility functions, in case anybody else can make use of them:


char* fstring2charp(FString highLevelString) {
	std::string midLevelString(TCHAR_TO_UTF8(*highLevelString));
	char* lowLevelString = new char[midLevelString.size()+1];
	std::copy(midLevelString.begin(), midLevelString.end(), lowLevelString);
	return lowLevelString;
}

FString charp2fstring(char* lowLevelString){
	std::string midLevelString(lowLevelString);
	FString highLevelString(FString(midLevelString.c_str()));
	return highLevelString;
}

I’m aware they are more verbose than they would need to be, I’ve left them like that for clarity’s sake. Also, I’m not exactly sure what happens on the memory management level; for instance, can we call delete on the resulting pointer of a call to fstring2charp?

Don’t forget to #include <string>, <algorithm>

It seems that when ā€˜UE4Str’ contains Chinese characters,this approach does not work well.

All Unicode characters have a problem when coerced into ascii characters. Your best bet is likely the macros I mentioned earlier.

What you want to be using is std::wstring, not std::string.

Just curious. It is said that UE4 doesn’t support std. How could you use std here?

While it’s not recommended to mix Unreal’s string containers and std::string, there’s nothing really stopping you from using the STL. Searching the engine source for ā€œstd::stringā€ does bring up a few results.

Thanks for your reply. I just met an issue when importing third party library with std:vector as input parameter and trying to wrap it. I had thought std is totally not allowed in UE4.

FString UE4Str = ā€œUE4 C++ åŒ…å«äø­ę–‡ā€;
//FString to std::string

std::string cstr(TCHAR_TO_UTF8(*UE4Str));

//std::string to FString éœ€č¦ä½æē”ØUTF8_TO_TCHAR åœØč½¬äø€ę¬”

ClientMessage(FString(UTF8_TO_TCHAR(cstr.c_str())));

2 Likes