# Conversion of Enum to String

**URL:** <https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869>\
**Category:** C++\
**Tags:** fstring, ENUM, unreal-engine, UE4, CPP, question\
**Created:** [November 8, 2015, 9:49pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869 "2015-11-08T21:49:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joelComberiati](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joelComberiati](https://forums.unrealengine.com/u/joelComberiati)\
**Post date:** [November 8, 2015, 9:49pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/1 "2015-11-08T21:49:14Z")

</div>

Hello,  
So I have been trying to convert my Enumeration values to an FString.

I followed this post: [A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums](https://wiki.unrealengine.com/Enums_For_Both_C%2B%2B_and_BP)

and ended up with this:

```
FString getBodyEnumAsString(EBodyPosition value){
	const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EBodyPosition"), true);
	if (!EnumPtr) return FString("Invalid");

	return EnumPtr->GetEnumName((int32)value);
}

```

This is not working though, something about unresolved externals. I am sure I am making some mistake somewhere, and if someone could help me with it, that would be great.

Thanks,

---

<div class="post-metadata">

**Author:** ![joelComberiati](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joelComberiati](https://forums.unrealengine.com/u/joelComberiati)\
**Post date:** [November 11, 2015, 6:34am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/2 "2015-11-11T06:34:45Z")

</div>

Still being an issue, any help?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_359f4d3c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_359f4d3c](https://forums.unrealengine.com/u/anonymous_user_359f4d3c)\
**Post date:** [November 11, 2015, 8:34am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/3 "2015-11-11T08:34:14Z")

</div>

I’m doing something similar for one of my enums and it works perfectly fine. Where do you declare the function and how does it get called?

---

<div class="post-metadata">

**Author:** ![joelComberiati](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joelComberiati](https://forums.unrealengine.com/u/joelComberiati)\
**Post date:** [November 13, 2015, 6:00am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/4 "2015-11-13T06:00:49Z")

</div>

I simply call it when i create my object, by just passing my Enum value through:  
getBodyEnumAsString(EBodyPosition::Waist);

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8a86028c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8a86028c](https://forums.unrealengine.com/u/anonymous_user_8a86028c)\
**Post date:** [March 14, 2016, 11:29am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/5 "2016-03-14T11:29:03Z")

</div>

> something about unresolved externals

would you clarify, is it compilation or run-time issue?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dacf621f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dacf621f](https://forums.unrealengine.com/u/anonymous_user_dacf621f)\
**Post date:** [June 1, 2016, 7:59pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/6 "2016-06-01T19:59:19Z")

</div>

template  
FString EnumToString(const FString& enumName, const T value, const FString& defaultValue) const  
{  
UEnum\* pEnum = FindObject(ANY\_PACKAGE, \*enumName, true);  
return pEnum  
? ExpandEnumString(pEnum-\>GetNameByIndex(static\_cast(value)).ToString(), enumName)  
: defaultValue;  
}

```
FString ExpandEnumString(const FString& name, const FString& enumName)
{
	FString expanded(name);
	FString spaceLetter("");
	FString spaceNumber("");
	FString search("");
	expanded.ReplaceInline(*enumName, TEXT(""), ESearchCase::CaseSensitive);
	expanded.ReplaceInline(TEXT("::"), TEXT(""), ESearchCase::CaseSensitive);
	for (TCHAR letter = 'A'; letter <= 'Z'; ++letter)
	{
		search = FString::Printf(TEXT("%c"), letter);
		spaceLetter = FString::Printf(TEXT(" %c"), letter);
		expanded.ReplaceInline(*search, *spaceLetter, ESearchCase::CaseSensitive);
	}
	for (TCHAR number = '0'; number <= '9'; ++number)
	{
		search = FString::Printf(TEXT("%c"), number);
		spaceNumber = FString::Printf(TEXT(" %c"), number);
		expanded.ReplaceInline(*search, *spaceNumber, ESearchCase::CaseSensitive);
	}
	expanded.ReplaceInline(TEXT("_"), TEXT(" -"), ESearchCase::CaseSensitive);
	expanded = expanded.RightChop(1).Trim().TrimTrailing();
	return expanded;
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_dacf621f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_dacf621f](https://forums.unrealengine.com/u/anonymous_user_dacf621f)\
**Post date:** [June 1, 2016, 8:03pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/7 "2016-06-01T20:03:12Z")

</div>

This will work in editor and game modes.

---

<div class="post-metadata">

**Author:** ![Kmack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kmack/32/706181_2.png) [@Kmack](https://forums.unrealengine.com/u/Kmack)\
**Post date:** [October 7, 2016, 10:44pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/8 "2016-10-07T22:44:17Z")

</div>

If you’d like to do this without creating a function to convert the enum, here’s a simple way to use the method outlined above:

```
void AMyGameWeapon::SetWeaponState(EWeaponState::Type NewState)
{
     const UEnum* WeaponStateEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT("EWeaponState"));
     UE_LOG(LogMyGameWeapon, Log, TEXT("SetWeaponState called on weapon %s with state %s (Role %s)")
    		, *GetNameSafe(this)
    		, *(WeaponStateEnum ? WeaponStateEnum->GetEnumName(NewState) : TEXT("<Invalid Enum>"))
    		, *UEnum::GetValueAsString(TEXT("Engine.ENetRole"), Role));

```

The first line finds a pointer to the enum object from its namespace, and then in the UE\_LOG call, we use that enum pointer to get a string value to output. Important to note is that the pointer isn’t guaranteed to be valid, so use a ternary operator to ensure that you safely handle a case where the pointer doesn’t exist.

This UE\_LOG statement also supplies an example of grabbing a string from an enum that isn’t enclosed in a namespace and whose path is known.

---

<div class="post-metadata">

**Author:** ![Missing\_A\_Few\_Heads](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Missing\_A\_Few\_Heads](https://forums.unrealengine.com/u/Missing_A_Few_Heads)\
**Post date:** [September 1, 2017, 5:36am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/9 "2017-09-01T05:36:35Z")

</div>

I think this is my first post… had to say… you are a freakin’ wizard. Why is this not in the engine? Thanks!

---

<div class="post-metadata">

**Author:** ![Missing\_A\_Few\_Heads](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Missing\_A\_Few\_Heads](https://forums.unrealengine.com/u/Missing_A_Few_Heads)\
**Post date:** [September 1, 2017, 5:57am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/10 "2017-09-01T05:57:17Z")

</div>

Using the info from both of these posts I wrote the following function that works:

```
template<typename T>
static FString EnumToString(const FString& enumName, const T value)
{
	UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, *enumName);
	return *(pEnum ? pEnum->GetNameStringByIndex(static_cast<uint8>(value)) : "null");
}

```

And I just put it in a header file along with my enums. Thanks yall!

---

<div class="post-metadata">

**Author:** ![92Rob](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@92Rob](https://forums.unrealengine.com/u/92Rob)\
**Post date:** [December 14, 2017, 12:11am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/11 "2017-12-14T00:11:27Z")

</div>

For the people that dislike the string parameter, I found a solution to this.  
I didn’t like the string because it would present problems when refactoring (renaming) the enum.

```
#define stringify( name ) # name

UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT(stringify(YourEnumType)), true);

```

The stringify macro literally converts the characters of its input to chars.  
this works great and it will refactor the enum because it’s now used as a type instead of a string.  
I hope this helps anyone.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f173db21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f173db21](https://forums.unrealengine.com/u/anonymous_user_f173db21)\
**Post date:** [July 10, 2018, 9:41am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/12 "2018-07-10T09:41:25Z")

</div>

Not working if enum is inside namespace or class.

---

<div class="post-metadata">

**Author:** ![Hyrex93](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hyrex93](https://forums.unrealengine.com/u/Hyrex93)\
**Post date:** [August 27, 2018, 5:47am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/13 "2018-08-27T05:47:50Z")

</div>

This is by far the most nice way to deal with Enum stringizing.

As for namespaces enum, you can try to declare using namespace within the implementation body before calling this macro. I’ve seen some usage like this.

```
namespace SomeNamespace;
{
  enum class EExample 
  {
    X,Y
  }
}

```

within implementation body

```
{
  using namespace SomeNamespace;
  stringify(EExample);
}

```

In addition, one can always write double macro to make it better. This was after referring @MissingAFewHead templated function.

```
#define stringify(name) #name
#define EnumValStr(Enum, Val) EnumToString( (stringify(Enum)) , Val)
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_3c18bd1f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_3c18bd1f](https://forums.unrealengine.com/u/anonymous_user_3c18bd1f)\
**Post date:** [August 21, 2019, 4:46am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/14 "2019-08-21T04:46:31Z")

</div>

Please just use:

```
DEFINE_ENUM_TO_STRING

```

e.g.

```
enum EMyEnum
{
}
DEFINE_ENUM_TO_STRING(EMyEnum)

```

then use

```
EnumToString(enumValue)

```

---

<div class="post-metadata">

**Author:** ![Kmack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kmack/32/706181_2.png) [@Kmack](https://forums.unrealengine.com/u/Kmack)\
**Post date:** [October 2, 2019, 11:26pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/15 "2019-10-02T23:26:43Z")

</div>

Update: GetEnumName is now deprecated. Use GetNameStringByIndex instead:

```
static const UEnum* MySpiffyEnumType = FindObject<UEnum>(ANY_PACKAGE, TEXT("EMySpiffyEnum"));
return MySpiffyEnumType->GetNameStringByIndex(static_cast<uint32>(Value));
```

---

<div class="post-metadata">

**Author:** ![Kmack](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kmack/32/706181_2.png) [@Kmack](https://forums.unrealengine.com/u/Kmack)\
**Post date:** [October 2, 2019, 11:32pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/16 "2019-10-02T23:32:13Z")

</div>

There’s a fairly simple way to do this that doesn’t require any special definitions.

- Use FindObject to find a pointer to your enum.

- Call GetNameStringByIndex() on that pointer.

---

<div class="post-metadata">

**Author:** ![AssemblerJohn](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/assemblerjohn/32/921539_2.png) [@AssemblerJohn](https://forums.unrealengine.com/u/AssemblerJohn)\
**Post date:** [December 9, 2020, 3:54pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/17 "2020-12-09T15:54:03Z")

</div>

Throwing my 2 cents here i’ve came with the following:

```
template<typename T>
static FString EnumToString(T Enum);

```

It only requires the enum type (absolutely no string and value). It can be used like this:

```
UYourFunctionLibrary::EnumToString<EBlockMode>(Mode);

```

Visual studio implementation (in GCC you can use **PRETTY\_FUNCTION** )

```
template<typename T>
	static FString EnumToString(T Enum)
	{
#if !WITH_EDITOR
		return "";
#else
		static FString EnumName;
		static bool bEnumNameInit = false;

		// Init it once
		if (bEnumNameInit == false)
		{
			bEnumNameInit = true;

			FString FunctionSignature( __FUNCSIG__ );
			UE_LOG(LogRPG, Display, TEXT("Enum funcsig: [%s]"), *FunctionSignature);

			int32 FirstIDX = FunctionSignature.Find(TEXT("(enum "), ESearchCase::CaseSensitive, ESearchDir::FromEnd);

			if (FirstIDX != INDEX_NONE)
			{
				FirstIDX = FirstIDX + 6;
				int32 LastIDX = (FunctionSignature.Len() - 1);

				if (FunctionSignature.IsValidIndex(FirstIDX) && FunctionSignature.IsValidIndex(LastIDX) && FirstIDX < LastIDX)
				{
					EnumName = FunctionSignature.Mid(FirstIDX, LastIDX - FirstIDX);
					UE_LOG(LogRPG, Display, TEXT("Enum name: [%s]"), *EnumName);
				}
			}
		}

		if (EnumName.IsEmpty())
			return FString("ENUM_Invalid");
		
		// const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, (const TCHAR *)(typeid(T).name()), true);
		const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, *EnumName, true);
		if (!EnumPtr)
			return FString("ENUM_Invalid");

		#pragma warning(disable: 4996)
		return EnumPtr->GetDisplayNameText(static_cast<int32>(Enum)).ToString();
#endif
}

```

I’m sure there’s some template master out there that can transform the usage of **FUNCSIG** into a constexpr, but the enum name is extracted only once. Note that I’m using it for debugging only, high performance is not a top priority for me.

---

<div class="post-metadata">

**Author:** ![gamebro2222](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gamebro2222](https://forums.unrealengine.com/u/gamebro2222)\
**Post date:** [January 27, 2021, 6:16am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/18 "2021-01-27T06:16:51Z")

</div>

There’s an extremely easy way to do this now.  
  
I forget what version it came in, I believe it was 4.24 or .23 or sooner.  
  
Check out this file.

```
Engine\Source\Runtime\CoreUObject\Public\UObject\ReflectedTypeAccessors.h

```

For enums it goes something like this.

```
StaticEnum<EnumTypeName>()

```

that object contains all the methods you need.  
  
You can get all the convert type values you’re looking for.  
  
A common one I use is

```
StaticEnum<EnumTypeName>()->GetValueAsString( EnumTypeName::EnumOnTheList );
```

---

<div class="post-metadata">

**Author:** ![Doug\_Richardson](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Doug\_Richardson](https://forums.unrealengine.com/u/Doug_Richardson)\
**Post date:** [February 3, 2021, 8:33am UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/19 "2021-02-03T08:33:57Z")

</div>

[UEnum::GetValueAsName](https://docs.unrealengine.com/en-US/API/Runtime/CoreUObject/UObject/UEnum/GetValueAsName/1/index.html) will give you an FName from a UENUM value.

For example:

```
UE_LOG(LogTemp, Log, TEXT("myenum: %s"), *UEnum::GetValueAsName(DORM_Awake).ToString());

```

prints:

```
LogMyRep: myenum: DORM_Awake

```

---

<div class="post-metadata">

**Author:** ![KKochiss](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KKochiss](https://forums.unrealengine.com/u/KKochiss)\
**Post date:** [February 28, 2021, 8:00pm UTC](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869/20 "2021-02-28T20:00:27Z")

</div>

This is Awesome! We need to get this moved to the top answer, it worked perfectly for me in 4.26!

[Next page](https://forums.unrealengine.com/t/conversion-of-enum-to-string/337869.md?page=2)
