ue4字符串 区别 文本_UE4学习笔记(22)C++输出字符串及FString字符串转换

本篇文章将和大家介绍UE4中C++输出字符串和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(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();

你可能感兴趣的:(ue4字符串,区别,文本)