关于 UE4 的字符串的类型转换
1. int32 与 FString 的互相转换
1 // int32 -> FString 2 int32 Nums = 123456789; 3 const FString Int32AToFString = FString::FromInt(Nums); 4 UE_LOG(LogTemp, Display, TEXT("Int32AToFString: %s"), *Int32AToFString); 5 6 // FString -> int32 7 const FString FStringA = TEXT("123456789"); 8 int32 FStringAToInt32 = FCString::Atoi(*FStringA); 9 UE_LOG(LogTemp, Display, TEXT("FStringAToInt32: %d"), FStringAToInt32);
2. int32 与 FString 转换 “3位分结” 计数法
1 // int32 -> FormatAsNumber 2 const int32 Nums = 123456789; 3 FString Int32AToFormatAsNumber = FString::FormatAsNumber(Nums); 4 UE_LOG(LogTemp, Display, TEXT("Int32AToFormatAsNumber: %s"), *Int32AToFormatAsNumber); 5 // FString -> FormatAsNumber 6 const FString FStringA = TEXT("123456789"); 7 FString FStringAToFormatAsNumber = FString::FormatAsNumber(FCString::Atoi(*FStringA)); 8 UE_LOG(LogTemp, Display, TEXT("FStringAToFormatAsNumber: %s"), *FStringAToFormatAsNumber);
3. FString 与 float 的互相转换
1 // FString -> Float 2 const FString StringA = TEXT("3.1415926"); 3 float StringToFloat = FCString::Atof(*StringA); 4 UE_LOG(LogTemp, Display, TEXT("StringToFloat: %.5f"), StringToFloat); 5 6 // Float -> FString 7 const float NumA = 3.1415926; 8 const FString FloatToString = FString::SanitizeFloat(NumA); 9 UE_LOG(LogTemp, Display, TEXT("FloatToString: %s"), *FloatToString);
4. FString 与 FName 的互相转换
1 // FName -> FString 2 FName FNameA("FNameA"); 3 FString FNameToFString = FNameA.ToString(); 4 UE_LOG(LogTemp, Display, TEXT("FNameToFString: %s"), *FNameToFString); 5 // FString -> FName 6 FString FStringA("FStringA"); 7 FName FStringToFName = *FStringA; 8 UE_LOG(LogTemp, Display, TEXT("FNameToFString: %s"), *FStringToFName.ToString());
·
5. FString 与 FText 的互相转换
1 //FString -> FText 2 FString FStringA("FStringA"); 3 FText FStringToFText = FText::FromString(FStringA); 4 UE_LOG(LogTemp, Display, TEXT("FStringToFText: %s"), *FStringToFText.ToString()); 5 //FText -> FString 6 FText TxtA = FText::AsNumber(1.4f); 7 FText TxtB = FText::FromString(TEXT("ABCDE")); //4.26以前的版本直接传入 TEXT("") 会导致崩溃 8 FText TxtC = FText::FromString(FString("FIJKL")); 9 FString TxtAToFStringA(TxtA.ToString()); 10 FString TxtBToFStringB(TxtB.ToString()); 11 FString TxtCToFStringC(TxtC.ToString()); 12 UE_LOG(LogTemp, Display, TEXT("TxtAToFStringA: %s"), *TxtAToFStringA); 13 UE_LOG(LogTemp, Display, TEXT("TxtBToFStringB: %s"), *TxtBToFStringB); 14 UE_LOG(LogTemp, Display, TEXT("TxtCToFStringC: %s"), *TxtCToFStringC);
6. TCHAR 与 ANSI_TO_TCHAR 转换
1 // 虚幻使用的字符类型为 TCHAR,采用 UTF-16 的编码格式。通过宏 ANSI_TO_TCHAR 转换将 ANSIC 码转换为虚幻的 TCHAR 类型 2 TCHAR* charsA = ANSI_TO_TCHAR("123"); 3 // 通过宏 TCHAR_TO_ANSI 将虚幻的 TCHAR 编码转为 ANSIC 码,注意:虚幻向外的输出一定只能是 TCHAR 类型的 4 ANSICHAR* ansiString = TCHAR_TO_ANSI(charsA); 5 // 对于 UTF-8 可以使用宏来进行来回转换 6 ANSICHAR* utf8String = TCHAR_TO_UTF8(charsA); 7 TCHAR* charsB = UTF8_TO_TCHAR(utf8String); 8 UE_LOG(LogTemp, Display, TEXT("%s"), charsA); 9 UE_LOG(LogTemp, Display, TEXT("%s"), ANSI_TO_TCHAR("234")); 10 UE_LOG(LogTemp, Display, TEXT("%s"), TEXT("345"));
![](https://img2022.cnblogs.com/blog/1872684/202208/1872684-20220811085236440-1639943923.png)
7. TCHAR* 与 FString 的隐式转换
1 // FString -> TCHAR* (TCHAR*与FString基本都能自动隐式转换) 2 const FString FStringToTCHAR(TEXT("FStringToTCHAR")); 3 UE_LOG(LogTemp, Display, TEXT("FStringToTCHARA: %s"), *FStringToTCHAR); 4 const TCHAR* TcharToString = *FStringToTCHAR; 5 UE_LOG(LogTemp, Display, TEXT("FStringToTCHARA->TcharToString: %s"), TcharToString);