UE4: Guidebook
4.24
4.24
  • Introduction
  • Quick Reference
    • C++ Data Type Snippets
    • The UPROPERTY Macro
    • The UFUNCTION Macro
  • Wiki Archives
    • Debugging & Utilities
      • Exec Functions
      • How To Prevent Crashes Due To Dangling Actor Pointers
      • Profiling: How to Count CPU Cycles
      • Logs: Printing Messages to Yourself during Runtime
    • Macros & Data Types
      • Structs, USTRUCTS(), They're Awesome
      • Enums For Both C++ and BP
      • Delegates in UE4, Raw C++, and BP Exposed
      • Interfaces in C++
      • Iterators
      • String Conversions: FString to FName, FString to Int32, Float to FString
    • Networking
      • Standalone Dedicated Server
      • How To Use Sessions In C++
      • Spawn Different Pawns For Players in Multiplayer
      • Spawn Different Pawns For Every Player
      • Gameplay Abilities and You
    • DevOps
      • Linking DLLs
    • AR & VR
      • Integrating OpenCV into Unreal Engine 4
Powered by GitBook
On this page
  • Overview
  • Converting FString to FNames
  • std::string to FString
  • FString to std::string
  • FCString Overview
  • Converting FString to Numbers
  • FString to Integer
  • FString to Float
  • Float/Integer to FString
  • UE4 Source Header References
  • Optimization Issues Concerning FNames
  • Authors

Was this helpful?

  1. Wiki Archives
  2. Macros & Data Types

String Conversions: FString to FName, FString to Int32, Float to FString

Guide on String conversions (from/to) by Rama the legend

PreviousIteratorsNextNetworking

Last updated 5 years ago

Was this helpful?

Content

Overview

Original Author: Rama

Below are conversions for the following types: 1. FString to FName 2. std::string to FString 3. FString and FCString Overview 4. FString to Integer 5. FString to Float 6. Float/Integer to FString 7. UE4 C++ Source Header References 8. Optimization Issues Concerning FNames

All the header files I refer to in this tutorial are found in

your UE4 install directory  / Engine / Source

you will probably want to do a search for them from this point :)

Converting FString to FNames

Say we have

FString TheString = "UE4_C++_IS_Awesome";

To convert this to an FName you do:

FName ConvertedFString = FName(*TheString);

std::string to FString

#include <string>
//....
std::string TestString = "Happy"; 
FString HappyString(TestString.c_str());

FString to std::string

#include <string>
//....
FString UE4Str = "Flowers";
std::string MyStdString(TCHAR_TO_UTF8(*UE4Str));

FCString Overview

Converting FString to Numbers

The operator on FStrings returns their TCHAR data which is what FCString functions use. If you cant find the function you want in FStrings (UnrealString.h) then you should check out the FCString functions (CString.h) I show how to convert from FString to FCString below: Say we have

FString TheString = "123.021";

FString to Integer

(note Atoi is unsafe; no way to indicate errors)

int32 MyShinyNewInt = FCString::Atoi(*TheString);

FString to Float

float MyShinyNewFloat = FCString::Atof(*TheString);

Note that Atoi and Atof are static functions, so you use the syntax FCString::TheFunction to call it :)

Float/Integer to FString

FString NewString = FString::FromInt(YourInt);
FString VeryCleanString = FString::SanitizeFloat(YourFloat);

Static functions in the UnrealString.h :)

UE4 Source Header References

CString.h
UnrealString.h
NameTypes.h

See CString.h for more details and other functions like

atoi64 (string to int64)
Atod    (string to double precision float)

For a great deal of helpful functions you will also want to look at UnrealString.h for direct manipulation of FStrings!

Optimization Issues Concerning FNames

FNames are inherently fast, but you could be forcing a hashmap lookup if you are accessing them in the wrong way. Look at the following code:

if (ActorHasTag(TEXT("MyFNameActor_Tag")))

This code will take the character string "MyFNameActor_Tag" and then look it up in the FName hashmap. Whereas this code doesn't need to do a string conversion:

static const FName NAME_MyFNameActor(TEXT("MyFNameActor_Tag"));
if (ActorHasTag(NAME_MyFNameActor))

In our testing with UE4 4.14, the second method is nearly 100 times faster than using the string lookup. So please, always use the static const FName method over the TEXT() method. For more info on FNames check out

NameTypes.h

Enjoy!

Authors

Original author: Rama <3 Minor Authors: Kory Captured from the epic wiki via the Wayback Machine. Reformatted by DarioMazzanti

You will find this particularly useful in cases other than float and int32! C++ std::String::to_string

http://en.cppreference.com/w/cpp/string/basic_string/to_string
Overview
Converting FString to FNames
std::string to FString
FString to std::string
FCString Overview
Converting FString to Numbers
FString to Integer
FString to Float
Float/Integer to FString
UE4 Source Header References
Optimization Issues Concerning FNames