UE4 常用类型转换

FString to FName 互转

//FString To FName
FString Str = TEXT("TestString");
FName Name = FName(*Str);
//FName To FString
FString TargetFString = Name.ToString();

FString to FText 互转

//FString to FText
FText MyText = FText::FromString(Str1);
//FText to FString 不可靠,在一些语言中存在损耗
FString MyString= MyText.ToString();  

FString to int32 互转

//FString to int32
int32 TestInt = FCString::Atoi(*MyFString);
//int32 to FString
FString TestString = FString::FromInt(TestInt);

FString to float 互转

//FString to float
float TestFloat = FCString::Atof(*MyFString);
//float to FString
FString TestString = FString::SanitizeFloat(TestFloat);

FString to std::string 互转

#include 
​
//FString to std::string
FString UE4Str = "Dog";
std::string MyStdString = TCHAR_TO_UTF8(*UE4Str);
//std::string to FString
FString HappyString(UTF8_TO_TCHAR(MyStdString.c_str()));

FString to char* 互转

//FString To char*
char* c = TCHAR_TO_UTF8(*MyStr);
//char* to FString
FString MyFString = FString(UTF8_TO_TCHAR(c));

FString to TCHAR* 互转

//FString to const TCHAR*
FString Str1(TEXT("TestString"));
TCHAR* MyTChar = *Str1;
//const TCHAR* to FString
FString Mystr = MyTChar;

FString to TArray 互转

#include "HAL/UnrealMemory.h"

//FString to TArray
FString str = "str";
TArray MyArr;
MyArr.SetNum(str.Len());
FMemory::Memcpy(MyArr.GetData(), TCHAR_TO_ANSI(*str), str.Len());
//TArray To FString
const std::string cstr1(reinterpret_cast(MyArr.GetData()), MyArr.Num());
FString  MyStr(UTF8_TO_TCHAR(cstr1.c_str()));
UE_LOG(LogTemp, Warning, TEXT("%s"), *MyStr);

 FString to enum 互转

enum ETestEnum
{
    East,
    South,
    West,
    North,
};
//enum to FString
const UEnum* EnumPtr = FindObject(ANY_PACKAGE, TEXT("ETestEnum"), true);
if (EnumPtr)
{
    FString str = EnumPtr->GetNameStringByIndex(ETestEnum::East);
}
//FString to enum  
const UEnum* EnumPtr = FindObject(ANY_PACKAGE, TEXT("ETestEnum"), true);
if (EnumPtr)
{
    ETestEnum OutEnum = (ETestEnum)EnumPtr->GetIndexByNameString(TEXT("North"));
}

TCHAR* to Char* 互转

//TCHAR* to char*
TCHAR* TCharString = TEXT("Hello, World!");
char* CharString = TCHAR_TO_ANSI(TCharString);
//char* to TCHAR*
TCHAR* TCharString1 = ANSI_TO_TCHAR(CharString);

TCHAR* to std::string

#include 

//TCHAR* to std::string
std::string str = TCHAR_TO_UTF8(TCharString);
//std::string to TCHAR*
TCHAR* TCStr = UTF8_TO_TCHAR(str);

TArray64 to TArray

//TArray64
//第一种
TArray ImgData = static_cast>(TArray64);
//第二种
TArray ImgData(TArray64);

你可能感兴趣的:(UE4,小知识,ue4)