UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StringConv.h File Reference
#include "CoreTypes.h"
#include "Misc/AssertionMacros.h"
#include "Containers/ContainerAllocationPolicies.h"
#include "Containers/Array.h"
#include "Misc/CString.h"
#include "Templates/IsArray.h"
#include "Templates/UnrealTemplate.h"
#include "Templates/UnrealTypeTraits.h"
#include "Traits/ElementType.h"
#include "Traits/IsCharEncodingCompatibleWith.h"
#include "Traits/IsContiguousContainer.h"
#include <type_traits>

Go to the source code of this file.

Classes

class  TStringConvert< From, To >
 
struct  UE::Core::Private::FCountingOutputIterator
 
class  UE::Core::Private::FTCHARToUTF8_Convert
 
struct  UE::Core::Private::FFromStrCast
 
class  FUTF8ToTCHAR_Convert
 
class  TUTF32ToUTF16_Convert< InFromType, InToType >
 
class  TUTF16ToUTF32_Convert< InFromType, InToType >
 
class  TStringConversion< Converter, DefaultConversionSize >
 
struct  TIsContiguousContainer< TStringConversion< Converter, DefaultConversionSize > >
 
struct  TElementType< TStringConversion< Converter, DefaultConversionSize > >
 
class  TStringPointer< InFromType, InToType >
 
struct  TIsContiguousContainer< TStringPointer< FromType, ToType > >
 
struct  TElementType< TStringPointer< FromType, ToType > >
 
class  TStringPassthru< ToType, FromType, DefaultConversionSize >
 
class  TPassthruPointer< T >
 

Namespaces

namespace  StringConv
 
namespace  UE
 
namespace  UE::Core
 
namespace  UE::Core::Private
 implementation
 

Macros

#define DEFAULT_STRING_CONVERSION_SIZE   128u
 
#define TCHAR_TO_ANSI(str)   /*UE_DEPRECATED_MACRO(5.xx, "TCHAR_TO_ANSI(Ptr) is deprecated, please use StringCast<ANSICHAR>(PtrToTChar) instead.")*/ (ANSICHAR*)StringCast<ANSICHAR>(static_cast<const TCHAR*>(str)).Get()
 
#define ANSI_TO_TCHAR(str)   /*UE_DEPRECATED_MACRO(5.xx, "ANSI_TO_TCHAR(Ptr) is deprecated, please use StringCast<TCHAR>(PtrToAnsiChar) instead.")*/ (TCHAR*)StringCast<TCHAR>(static_cast<const ANSICHAR*>(str)).Get()
 
#define TCHAR_TO_UTF8(str)   /*UE_DEPRECATED_MACRO(5.xx, "TCHAR_TO_UTF8(Ptr) is deprecated, please use StringCast<UTF8CHAR>(PtrToTChar) instead.")*/ (ANSICHAR*)FTCHARToUTF8((const TCHAR*)str).Get()
 
#define UTF8_TO_TCHAR(str)   /*UE_DEPRECATED_MACRO(5.xx, "UTF8_TO_TCHAR(Ptr) is deprecated, please use StringCast<TCHAR>(PtrToUTF8Char) instead.")*/ (TCHAR*)FUTF8ToTCHAR((const ANSICHAR*)str).Get()
 
#define TCHAR_TO_UTF16(str)   (UTF16CHAR*)(str)
 
#define UTF16_TO_TCHAR(str)   (TCHAR*)(str)
 
#define TCHAR_TO_UTF32(str)   (UTF32CHAR*)FTCHARToUTF32((const TCHAR*)str).Get()
 
#define UTF32_TO_TCHAR(str)   (TCHAR*)FUTF32ToTCHAR((const UTF32CHAR*)str).Get()
 
#define TCHAR_TO_WCHAR(str)   (wchar_t*)(str)
 
#define WCHAR_TO_TCHAR(str)   (TCHAR*)(str)
 

Typedefs

using FTCHARToUTF8_Convert = UE::Core::Private::FTCHARToUTF8_Convert
 
using FTCHARToUTF8 = TStringConversion< UE::Core::Private::FTCHARToUTF8_Convert >
 
using FUTF8ToTCHAR = TStringConversion< FUTF8ToTCHAR_Convert >
 
typedef TStringPointer< TCHAR, UTF16CHARFTCHARToUTF16
 
typedef TStringPointer< UTF16CHAR, TCHARFUTF16ToTCHAR
 
typedef TStringConversion< TUTF16ToUTF32_Convert< TCHAR, UTF32CHAR > > FTCHARToUTF32
 
typedef TStringConversion< TUTF32ToUTF16_Convert< UTF32CHAR, TCHAR > > FUTF32ToTCHAR
 
typedef TStringPointer< TCHAR, wchar_tFTCHARToWChar
 
typedef TStringPointer< wchar_t, TCHARFWCharToTCHAR
 

Enumerations

enum class  UE::Core::Private::ENullTerminatedString { UE::Core::Private::No = 0 , UE::Core::Private::Yes = 1 }
 

Functions

bool StringConv::IsValidCodepoint (const uint32 Codepoint)
 
UE_FORCEINLINE_HINT bool StringConv::IsHighSurrogate (const uint32 Codepoint)
 
UE_FORCEINLINE_HINT bool StringConv::IsLowSurrogate (const uint32 Codepoint)
 
UE_FORCEINLINE_HINT uint32 StringConv::EncodeSurrogate (const uint16 HighSurrogate, const uint16 LowSurrogate)
 
void StringConv::DecodeSurrogate (const uint32 Codepoint, uint16 &OutHighSurrogate, uint16 &OutLowSurrogate)
 
UE_FORCEINLINE_HINT bool StringConv::IsEncodedSurrogate (const uint32 Codepoint)
 
template<typename CharType >
int32 StringConv::InlineCombineSurrogates_Buffer (CharType *StrBuffer, int32 StrLen)
 
template<typename AllocatorType >
void StringConv::InlineCombineSurrogates_Array (TArray< TCHAR, AllocatorType > &StrBuffer)
 
template<typename ConverterType >
ConverterType::LegacyFromType StringConv::GetLegacyFromType (typename ConverterType::LegacyFromType *)
 
template<typename ConverterType >
FUnused StringConv::GetLegacyFromType (...)
 
template<typename Converter , int32 DefaultConversionSize>
auto GetData (const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
 
template<typename Converter , int32 DefaultConversionSize>
auto GetNum (const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Length())
 
template<typename FromType , typename ToType >
auto GetData (const TStringPointer< FromType, ToType > &Pointer) -> decltype(Pointer.Get())
 
template<typename FromType , typename ToType >
auto GetNum (const TStringPointer< FromType, ToType > &Pointer) -> decltype(Pointer.Length())
 
template<typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE, typename From >
auto StringCast (const From *Str)
 
template<typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE, typename From >
auto StringCast (const From *Str, int32 Len)
 
template<typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE, typename From >
auto StrCast (const From *Str, int32 Len)
 
template<typename To , typename From >
To CharCast (From Ch)
 
template<typename From , typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE>
auto StringMemoryPassthru (To *Buffer, int32 BufferSize, int32 SourceLength)
 
template<typename ToType , typename FromType >
TArray< ToType > StringToArray (const FromType *Src, int32 SrcLen)
 
template<typename ToType , typename FromType >
UE_FORCEINLINE_HINT TArray< ToType > StringToArray (const FromType *Str)
 

Macro Definition Documentation

◆ ANSI_TO_TCHAR

#define ANSI_TO_TCHAR (   str)    /*UE_DEPRECATED_MACRO(5.xx, "ANSI_TO_TCHAR(Ptr) is deprecated, please use StringCast<TCHAR>(PtrToAnsiChar) instead.")*/ (TCHAR*)StringCast<TCHAR>(static_cast<const ANSICHAR*>(str)).Get()

◆ DEFAULT_STRING_CONVERSION_SIZE

#define DEFAULT_STRING_CONVERSION_SIZE   128u

◆ TCHAR_TO_ANSI

#define TCHAR_TO_ANSI (   str)    /*UE_DEPRECATED_MACRO(5.xx, "TCHAR_TO_ANSI(Ptr) is deprecated, please use StringCast<ANSICHAR>(PtrToTChar) instead.")*/ (ANSICHAR*)StringCast<ANSICHAR>(static_cast<const TCHAR*>(str)).Get()

◆ TCHAR_TO_UTF16

#define TCHAR_TO_UTF16 (   str)    (UTF16CHAR*)(str)

◆ TCHAR_TO_UTF32

#define TCHAR_TO_UTF32 (   str)    (UTF32CHAR*)FTCHARToUTF32((const TCHAR*)str).Get()

◆ TCHAR_TO_UTF8

#define TCHAR_TO_UTF8 (   str)    /*UE_DEPRECATED_MACRO(5.xx, "TCHAR_TO_UTF8(Ptr) is deprecated, please use StringCast<UTF8CHAR>(PtrToTChar) instead.")*/ (ANSICHAR*)FTCHARToUTF8((const TCHAR*)str).Get()

◆ TCHAR_TO_WCHAR

#define TCHAR_TO_WCHAR (   str)    (wchar_t*)(str)

◆ UTF16_TO_TCHAR

#define UTF16_TO_TCHAR (   str)    (TCHAR*)(str)

◆ UTF32_TO_TCHAR

#define UTF32_TO_TCHAR (   str)    (TCHAR*)FUTF32ToTCHAR((const UTF32CHAR*)str).Get()

◆ UTF8_TO_TCHAR

#define UTF8_TO_TCHAR (   str)    /*UE_DEPRECATED_MACRO(5.xx, "UTF8_TO_TCHAR(Ptr) is deprecated, please use StringCast<TCHAR>(PtrToUTF8Char) instead.")*/ (TCHAR*)FUTF8ToTCHAR((const ANSICHAR*)str).Get()

◆ WCHAR_TO_TCHAR

#define WCHAR_TO_TCHAR (   str)    (TCHAR*)(str)

Typedef Documentation

◆ FTCHARToUTF16

◆ FTCHARToUTF32

◆ FTCHARToUTF8

NOTE: The objects these macros declare have very short lifetimes. They are meant to be used as parameters to functions. You cannot assign a variable to the contents of the converted string as the object will go out of scope and the string released.

NOTE: The parameter you pass in MUST be a proper string, as the parameter is typecast to a pointer. If you pass in a char, not char* it will compile and then crash at runtime.

Usage:

SomeApi(TCHAR_TO_ANSI(SomeUnicodeString));

const char* SomePointer = TCHAR_TO_ANSI(SomeUnicodeString); <--- Bad!!!

◆ FTCHARToUTF8_Convert

◆ FTCHARToWChar

◆ FUTF16ToTCHAR

◆ FUTF32ToTCHAR

◆ FUTF8ToTCHAR

◆ FWCharToTCHAR

Function Documentation

◆ CharCast()

template<typename To , typename From >
To CharCast ( From  Ch)
inline

Casts one fixed-width char type into another.

Parameters
ChThe character to convert.
Returns
The converted character.

◆ GetData() [1/2]

template<typename Converter , int32 DefaultConversionSize>
auto GetData ( const TStringConversion< Converter, DefaultConversionSize > &  Conversion) -> decltype(Conversion.Get())
inline

◆ GetData() [2/2]

template<typename FromType , typename ToType >
auto GetData ( const TStringPointer< FromType, ToType > &  Pointer) -> decltype(Pointer.Get())
inline

◆ GetNum() [1/2]

template<typename Converter , int32 DefaultConversionSize>
auto GetNum ( const TStringConversion< Converter, DefaultConversionSize > &  Conversion) -> decltype(Conversion.Length())
inline

◆ GetNum() [2/2]

template<typename FromType , typename ToType >
auto GetNum ( const TStringPointer< FromType, ToType > &  Pointer) -> decltype(Pointer.Length())
inline

◆ StrCast()

template<typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE, typename From >
auto StrCast ( const From *  Str,
int32  Len 
)
inline

Creates an object which acts as a source of a given string type. See example above.

This is intended as the long-term replacement for StringCast, which doesn't cope well with zeros mid-string, which it can interpret as a null-terminators and give surprising behavior.

StrCast expects correctly-typed strings. If a cast is attempted with a char* or ANSICHAR*, and the string contains characters that are non-ASCII, including UTF-8 code units outside of the 7-bit ASCII range, then those values will fail to convert and a bogus char will be written in their place.

If a conversion from UTF-8 is desired, the pointer should be cast to UTF8CHAR* before being passed to StrCast.

Similarly, doing a StrCast<char> or StrCast<ANSICHAR> on a Unicode string will only successfully convert Unicode characters which already lie in the ASCII range. For converting to UTF-8, StrCast<UTF8CHAR> should be used.

The source string must not be modified or destroyed until after the result of this function has been destroyed.

Parameters
StrA pointer to the start of the string to convert. Must be non-null if Len is non-zero.
LenThe number of From elements in Str. Must be non-negative.

◆ StringCast() [1/2]

template<typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE, typename From >
auto StringCast ( const From *  Str)
inline

StringCast example usage:

void Func(const FString& Str, const TCHAR* MediumLong, FStringView MediumLongSV) { // Basic version auto Src = StringCast<ANSICHAR>(<em>Str); const ANSICHAR Ptr = Src.Get(); // Ptr is a pointer to an ANSICHAR representing the potentially-converted string data. // Avoid calling strlen auto NoStrlenString = StringCast<ANSICHAR>(MediumLongSV.GetData(), MediumLongSV.Len()); // Specify the static allocation size when maximum length is known auto NoAllocSrc = StringCast<ANSICHAR, 1024>(MediumLong); // Avoid calling strlen and also specify the allocation size auto NoAllocNoStrlenSrc = StringCast<ANSICHAR, 1024>(MediumLongSV.GetData(), MediumLongSV.Len()); } Creates an object which acts as a source of a given string type. See example above.

StringCast expects correctly-typed strings. If a cast is attempted with a char* or ANSICHAR*, and the string contains characters that are non-ASCII, including UTF-8 code units outside of the 7-bit ASCII range, then those values will fail to convert and a bogus char will be written in their place.

If a conversion from UTF-8 is desired, the pointer should be cast to UTF8CHAR* before being passed to StringCast.

Similarly, doing a StringCast<char> or StringCast<ANSICHAR> on a Unicode string will only successfully convert Unicode characters which already lie in the ASCII range. For converting to UTF-8, StringCast<UTF8CHAR> should be used.

The source string must not be modified or destroyed until after the result of this function has been destroyed.

Parameters
StrThe null-terminated source string to convert.

◆ StringCast() [2/2]

template<typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE, typename From >
auto StringCast ( const From *  Str,
int32  Len 
)
inline

Creates an object which acts as a source of a given string type. See example above.

StringCast expects correctly-typed strings. If a cast is attempted with a char* or ANSICHAR*, and the string contains characters that are non-ASCII, including UTF-8 code units outside of the 7-bit ASCII range, then those values will fail to convert and a bogus char will be written in their place.

If a conversion from UTF-8 is desired, the pointer should be cast to UTF8CHAR* before being passed to StringCast.

Similarly, doing a StringCast<char> or StringCast<ANSICHAR> on a Unicode string will only successfully convert Unicode characters which already lie in the ASCII range. For converting to UTF-8, StringCast<UTF8CHAR> should be used.

The source string must not be modified or destroyed until after the result of this function has been destroyed.

Parameters
StrThe source string to convert, not necessarily null-terminated.
LenThe number of From elements in Str.

◆ StringMemoryPassthru()

template<typename From , typename To , int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE>
auto StringMemoryPassthru ( To *  Buffer,
int32  BufferSize,
int32  SourceLength 
)
inline

Allows the efficient conversion of strings by means of a temporary memory buffer only when necessary. Intended to be used when you have an API which populates a buffer with some string representation which is ultimately going to be stored in another representation, but where you don't want to do a conversion or create a temporary buffer for that string if it's not necessary.

Intended use:

// Populates the buffer Str with StrLen characters. void SomeAPI(APICharType* Str, int32 StrLen);

void Func(DestChar* Buffer, int32 BufferSize) { // Create a passthru. This takes the buffer (and its size) which will ultimately hold the string, as well as the length of the // string that's being converted, which must be known in advance. // An explicit template argument is also passed to indicate the character type of the source string. // Buffer must be correctly typed for the destination string type. auto Passthru = StringMemoryPassthru<APICharType>(Buffer, BufferSize, SourceLength);

// Passthru.Get() returns an APICharType buffer pointer which is guaranteed to be SourceLength characters in size. // It's possible, and in fact intended, for Get() to return the same pointer as Buffer if DestChar and APICharType are // compatible string types. If this is the case, SomeAPI will write directly into Buffer. If the string types are not // compatible, Get() will return a pointer to some temporary memory which allocated by and owned by the passthru. SomeAPI(Passthru.Get(), SourceLength);

// If the string types were not compatible, then the passthru used temporary storage, and we need to write that back to Buffer. // We do that with the Apply call. If the string types were compatible, then the data was already written to Buffer directly // and so Apply is a no-op. Passthru.Apply();

// Now Buffer holds the data output by SomeAPI, already converted if necessary. }

◆ StringToArray() [1/2]

template<typename ToType , typename FromType >
TArray< ToType > StringToArray ( const FromType *  Src,
int32  SrcLen 
)
inline

◆ StringToArray() [2/2]

template<typename ToType , typename FromType >
UE_FORCEINLINE_HINT TArray< ToType > StringToArray ( const FromType *  Str)