【UE4学习】22_C++输出字符串及FString字符串转换

  • C输出字符串
    • 输出字符串到output log中
    • 输出字符串到屏幕上
  • 字符串转换
    • Converting FString to FNames
    • Converting stdstring to FString
    • Converting FString to stdstring
    • Converting FString to Integer
    • Converting FString to Float
    • Converting FloatInteger to FString
      • UE4 Source Header References
    • Converting FArrayReaderPtr to FString
    • Converting Array to FString
    • Converting FString to Array
    • Converting FString to char
    • Converting FString to const char
    • Converting FString to TCHAR
    • Converting FString to FText
    • Converting FText to FString
  • 参考


C++输出字符串

输出字符串到output log中

FString filePath
UE_LOG(LogTemp, Log, TEXT("%s"), *filePath);

UE_LOG(LogTemp, Error, TEXT("Hello,World!"));

输出字符串到屏幕上

FString filePath;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("DLL_Init")));

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("%s"), *filePath));

字符串转换

Converting FString to FNames

FString TheString = "UE4_C++_IS_Awesome";
FName ConvertedFString = FName(*TheString);

Converting std::string to FString

#include 

//....
std::string TestString = "Happy"; 
FString HappyString(TestString.c_str());

Converting FString to std::string

#include 

//....
FString UE4Str = "Flowers";
std::string MyStdString(TCHAR_TO_UTF8(*UE4Str));
  • 数值转std::to_string
    • std::string to_string( long value );
    • std::string to_string( long long value );
    • std::string to_string( unsigned value );
    • std::string to_string( unsigned long value );
    • std::string to_string( unsigned long long value );
    • std::string to_string( float value );
    • std::string to_string( double value );
    • std::string to_string( long double value );

Converting FString to Integer

FString TheString = "123.021";
int32 MyShinyNewInt = FCString::Atoi(*TheString);

Converting FString to Float

FString TheString = "123.021";
int32 MyShinyNewInt = FCString::Atof(*TheString);

Converting Float/Integer to FString

FString NewString = FString::FromInt(YourInt);
FString VeryCleanString = FString::SanitizeFloat(YourFloat);

UE4 Source Header References

CString.h
UnrealString.h
NameTypes.h
StringConv.h (TCHAR_TO_ANSI etc)

CString.h可以查到更多,如

atoi64 (string to int64)
Atod    (string to double precision float)

更多关于FString的操作可以到UnrealString.h找到.

Converting FArrayReaderPtr to FString

uint8 data[512];  
FMemory::Memzero(data, 512);  

FMemory::Memcpy(data, ArrayReaderPtr->GetData(), ArrayReaderPtr->Num());  
FString str = ((const char*)data);  

Converting Array to FString

TArray content;  
...  
const std::string cstr(reinterpret_cast<const char*>(content.GetData()), content.Num());  
FString frameAsFString = cstr.c_str();  
UE_LOG(VRSLog, Warning, TEXT("%s"), *frameAsFString);  

Converting FString to Array

FString JsonStr;  
TArray content;  

content.SetNum(JsonStr.Len());  
memcpy(content.GetData(), TCHAR_TO_ANSI(*JsonStr), JsonStr.Len());   
FString StrData;  
const TCHAR* StrPtr = *StrData;  
FTCHARToUTF8 UTF8String(StrPtr);  
int32 CTXSize = UTF8String.Length();  

TArray URLData;  
URLData.SetNum(CTXSize);  
memcpy(URLData.GetData(), UTF8String.Get(), CTXSize);  

Converting FString to char *

///1
FString str
string t = TCHAR_TO_UTF8(*str);
char * returnvalue = (char *)malloc(sizeof(char) * (t.length() + 1));
strncpy_s(returnvalue, t.length() , t.c_str(), t.length());  

///2
int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
{
    return GetColumnIndex(resultSet, TCHAR_TO_ANSI(*columnName));
}

int ARPG_Database::GetColumnIndex(int iResult, const char* columnName)
{
}

Converting FString to const char *

const char *plainText= TCHAR_TO_ANSI(*aes_content);

Converting FString to TCHAR *

 int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
 {
     return GetColumnIndex(resultSet, *columnName);
 }

 int ARPG_Database::GetColumnIndex(int iResult, const TCHAR* columnName)
 {
 }

Converting FString to FText

FString Str = TEXT("str");  
FText Text = FText::FromString(Str);

Converting FText to FString

FString Name = NameDesc->GetText().ToString();  

参考

  • Logs, Printing Messages To Yourself During Runtime

  • UEngine::AddOnScreenDebugMessage

  • String Conversions: FString to FName, FString to Int32, Float to FString

  • 字符编码

  • String Casting Fun in Unreal Engine 4 UPDATED

  • 虚幻4学习—UE4中的字符串转换

你可能感兴趣的:(Unreal,Engine)