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 <string>
//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<uint8> 互转
#include "HAL/UnrealMemory.h"
//FString to TArray<uint8>
FString str = "str";
TArray<uint8> MyArr;
MyArr.SetNum(str.Len());
FMemory::Memcpy(MyArr.GetData(), TCHAR_TO_ANSI(*str), str.Len());
//TArray<uint8> To FString
const std::string cstr1(reinterpret_cast<const char*>(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<UEnum>(ANY_PACKAGE, TEXT("ETestEnum"), true);
if (EnumPtr)
{
FString str = EnumPtr->GetNameStringByIndex(ETestEnum::East);
}
//FString to enum
const UEnum* EnumPtr = FindObject<UEnum>(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 <string>
//TCHAR* to std::string
std::string str = TCHAR_TO_UTF8(TCharString);
//std::string to TCHAR*
TCHAR* TCStr = UTF8_TO_TCHAR(str);
TArray64<uint8> to TArray<uint8>
//TArray64<uint8>
//第一种
TArray<uint8> ImgData = static_cast<TArray<uint8, FDefaultAllocator>>(TArray64<uint8>);
//第二种
TArray<uint8> ImgData(TArray64<uint8>);
文章来源地址https://www.toymoban.com/news/detail-442889.html
文章来源:https://www.toymoban.com/news/detail-442889.html
到了这里,关于UE4 常用类型转换的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!