UE4:常用FString类型转换方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
FString string = "string";
FName fname = FName(*string);// FString 转 FName
TCHAR tchar = *string;//FString 转 TChar*
FText ftext = FText::FromString(string);//FString 转 FText
std::string cstring(TCHAR_TO_UTF8(*string));//FString 转 C++ string
FString numberString = "1234.123";
int32 strint = FCString::Atoi(*numberString);//FString 转 int32
float strfloat = FCString::Atof(*numberString);//FString 转 float
bool strBool = string.ToBool();//转bool

//FString 转 TArray<uint8>
TArray<uint8> uint8Array;
uint8Array.SetNum(string.len());
memcpy(uint8Array.GetData(), TCHAR_TO_ANSI(*string), string.Len());

//TArray<uint8> 转 FString
TArray<uint8> content;
const std::string cstr(reinterpret_cast<const char*>(content.GetData()), content.Num());
FString frameAsFString = cstr.c_str();

//C++ string 转 FString
std::string cstring = "cstring";
FString fromCstring(cstring.c_str());

FString fromFText = ftext.ToString();//FText 转 FString
FStirng fromFName = fname.ToString();//FName 转 FString
Fstring fromfloat = FString::SanitizeFloat(strfloat);//float 转
FString fromint = FString::FromInt(IntVariable);//int
FString frombool = InBool ? TEXT("true") : TEXT("false");//bool
FString fromFVector = VectorVariable.ToString();//Vector
FString fromFVector2D = Vector2DVariable.ToString();//Vector2D
FString fromFRotator = RotatorVariable.ToString();//FRotator
FString fromFLinerColor = LinearColorVariable.ToString();//FLinerColor
FString fromUObject = (InObj != NULL) ? InObj->GetName() : FString(TEXT("None"));//UObject

UE4:常用FString类型转换方法
http://muchenhen.com/posts/50255/
作者
木尘痕
发布于
2020年8月25日
许可协议