【UE4学习】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!"));
  • 1
  • 2
  • 3
  • 4

输出字符串到屏幕上

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));
  • 1
  • 2
  • 3
  • 4

字符串转换

Converting FString to FNames

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

Converting std::string to FString

#include 

//....
std::string TestString = "Happy"; 
FString HappyString(TestString.c_str());
  • 1
  • 2
  • 3
  • 4
  • 5

Converting FString to std::string

#include 

//....
FString UE4Str = "Flowers";
std::string MyStdString(TCHAR_TO_UTF8(*UE4Str));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 数值转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);
  • 1
  • 2

Converting FString to Float

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

Converting Float/Integer to FString

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

UE4 Source Header References

CString.h
UnrealString.h
NameTypes.h
StringConv.h (TCHAR_TO_ANSI etc)
  • 1
  • 2
  • 3
  • 4

CString.h可以查到更多,如

atoi64 (string to int64)
Atod    (string to double precision float)
  • 1
  • 2

更多关于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);  
  • 1
  • 2
  • 3
  • 4
  • 5

Converting Array to FString

TArray content;  
...  
const std::string cstr(reinterpret_cast(content.GetData()), content.Num());  
FString frameAsFString = cstr.c_str();  
UE_LOG(VRSLog, Warning, TEXT("%s"), *frameAsFString);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Converting FString to Array

FString JsonStr;  
TArray content;  

content.SetNum(JsonStr.Len());  
memcpy(content.GetData(), TCHAR_TO_ANSI(*JsonStr), JsonStr.Len());   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
FString StrData;  
const TCHAR* StrPtr = *StrData;  
FTCHARToUTF8 UTF8String(StrPtr);  
int32 CTXSize = UTF8String.Length();  

TArray URLData;  
URLData.SetNum(CTXSize);  
memcpy(URLData.GetData(), UTF8String.Get(), CTXSize);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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)
{
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Converting FString to const char *

const char *plainText= TCHAR_TO_ANSI(*aes_content);
  • 1

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)
 {
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Converting FString to FText

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

Converting FText to FString

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

参考

  • 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中的字符串转换

你可能感兴趣的:(ue4蓝图)