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.
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
Rama
PS: Bob_Gneu I really like your quote in your signature
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!
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
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>
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!
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())));