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
Author: 木尘痕
Link: https://muchenhen.com/2020/08/25/UE4-%E5%B8%B8%E7%94%A8FString%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E6%96%B9%E6%B3%95/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.