# Convert std::string to FString

**URL:** <https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [May 27, 2014, 3:04am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065 "2014-05-27T03:04:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amishashashin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Amishashashin](https://forums.unrealengine.com/u/Amishashashin)\
**Post date:** [May 27, 2014, 3:04am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/1 "2014-05-27T03:04:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bob\_Gneu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bob\_Gneu](https://forums.unrealengine.com/u/Bob_Gneu)\
**Post date:** [May 27, 2014, 3:17am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/2 "2014-05-27T03:17:12Z")

</div>

> [@Amishashashin;59691](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [May 27, 2014, 4:31am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/4 "2014-05-27T04:31:49Z")

</div>

To add more to the discussion:

I had no trouble just doing this:

```auto

#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 ❤

---

<div class="post-metadata">

**Author:** ![Amishashashin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Amishashashin](https://forums.unrealengine.com/u/Amishashashin)\
**Post date:** [May 27, 2014, 6:07am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/5 "2014-05-27T06:07:32Z")

</div>

Thanks guys!

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

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [May 30, 2014, 11:06am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/6 "2014-05-30T11:06:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5cad28ab](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5cad28ab](https://forums.unrealengine.com/u/anonymous_user_5cad28ab)\
**Post date:** [December 7, 2014, 10:55am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/7 "2014-12-07T10:55:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bob\_Gneu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bob\_Gneu](https://forums.unrealengine.com/u/Bob_Gneu)\
**Post date:** [December 15, 2014, 11:47pm UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/8 "2014-12-15T23:47:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [December 16, 2014, 7:37am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/9 "2014-12-16T07:37:02Z")

</div>

**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!

```auto

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\>

---

<div class="post-metadata">

**Author:** ![Ventura](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ventura](https://forums.unrealengine.com/u/Ventura)\
**Post date:** [March 30, 2015, 10:43am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/10 "2015-03-30T10:43:07Z")

</div>

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:

```auto

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](http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char/4152881#4152881) (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?

```auto

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.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 30, 2015, 11:28am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/11 "2015-03-30T11:28:16Z")

</div>

> [@Ventura;260089](#):
>
> Hi Rama (and everybody)!

Hi there!

> [@Ventura;260089](#):
>
> I must admit I’ve been lurking in the forums for quite some time now, but I never dared to post.

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

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

> [@Ventura;260089](#):
>
> P.S.: Thanks for EVERYTHING you post, it’s a source of inspiration and it is always helpful.

You’re welcome!

🙂

````auto

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
````

---

<div class="post-metadata">

**Author:** ![Ventura](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ventura](https://forums.unrealengine.com/u/Ventura)\
**Post date:** [March 30, 2015, 4:40pm UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/12 "2015-03-30T16:40:40Z")

</div>

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:

```auto

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\>

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_71046e02](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_71046e02](https://forums.unrealengine.com/u/anonymous_user_71046e02)\
**Post date:** [May 4, 2015, 7:38am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/13 "2015-05-04T07:38:24Z")

</div>

> [@Rama;260110](#):
>
> Hi there!
> 
> FString UE4Str = “UE4 C++”;
> 
> //FString to std::string  
> std::string Your\_Cstr(TCHAR\_TO\_UTF8(\*UE4Str)):
> 
> //char\*  
> const char \* ReadOnlyCharPtr = Your\_Cstr.c\_str();
> 
> ```auto
> 
> Rama
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Bob\_Gneu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bob\_Gneu](https://forums.unrealengine.com/u/Bob_Gneu)\
**Post date:** [May 5, 2015, 1:52am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/14 "2015-05-05T01:52:03Z")

</div>

> [@mayaping;284360](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Sir\_Rogers](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sir\_Rogers](https://forums.unrealengine.com/u/Sir_Rogers)\
**Post date:** [December 28, 2015, 5:22am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/15 "2015-12-28T05:22:12Z")

</div>

> [@mayaping;284360](#):
>
> It seems that when ‘UE4Str’ contains Chinese characters,this approach does not work well.

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

> [@](#):
>
> FString UE4Str = “Your Chinese Characters Here”;
> 
> //FString to std::wstring
> 
> std::wstring w(\*UE4Str):

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ec2d883c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ec2d883c](https://forums.unrealengine.com/u/anonymous_user_ec2d883c)\
**Post date:** [April 8, 2016, 4:56am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/16 "2016-04-08T04:56:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sswires](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sswires](https://forums.unrealengine.com/u/sswires)\
**Post date:** [April 8, 2016, 8:55am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/17 "2016-04-08T08:55:06Z")

</div>

> [@Rock Luo;508599](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ec2d883c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ec2d883c](https://forums.unrealengine.com/u/anonymous_user_ec2d883c)\
**Post date:** [April 10, 2016, 7:12am UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/18 "2016-04-10T07:12:14Z")

</div>

> [@sswires;508680](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![ningbeibei](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ningbeibei](https://forums.unrealengine.com/u/ningbeibei)\
**Post date:** [February 13, 2020, 1:17pm UTC](https://forums.unrealengine.com/t/convert-std-string-to-fstring/5065/20 "2020-02-13T13:17:03Z")

</div>

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())));
