[UE4] 虚幻4学习---UE4中的字符串转换

String Conversions: 

FString To FName

FString To Int32

Float To FString

FArrayReaderPtr To FString

TArray<uint8> To FString

FString To char *  ---  (  TCHAR_TO_ANSI()  )

FString To TCHAR *

FString To Array<uint8>

FString To FText (新增)

FText To FString (新增)

Overview

  1. FString to FName
  2. std::string to FString
  3. FString and FCString Overview
  4. FString to Integer
  5. FString to Float
  6. Float/Integer to FString
  7. UE4 C++ Source Header References

All the header files I refer to in this tutorial are found in

your UE4 install directory  / Engine / Source

you will probably want to do a search for them from this point :)

Converting FString to FNames

Say we have

FString TheString = "UE4_C++_IS_Awesome";

To convert this to an FName you do:

FName ConvertedFString = FName(*TheString);

std::string to FString

#include <string>
 
//....
 
some function
{
  std::string TestString = "Happy"; 
  FString HappyString(TestString.c_str());
}

FString to std::string

#include <string>
 
//....
FString UE4Str = "Flowers";
std::string MyStdString(TCHAR_TO_UTF8(*UE4Str));

FCString Overview

Converting FString to Numbers

The * operator on FStrings returns their TCHAR* data which is what FCString functions use.

If you cant find the function you want in FStrings (UnrealString.h) then you should check out the FCString functions (CString.h)

I show how to convert from FString to FCString below:

Say we have

FString TheString = "123.021";

FString to Integer

int32 MyShinyNewInt = FCString::Atoi(*TheString);

FString to Float

float MyShinyNewFloat = FCString::Atof(*TheString);

Note that Atoi and Atof are static functions, so you use the syntax FCString::TheFunction to call it :)

 

Float/Integer to FString

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

Static functions in the UnrealString.h :)

UE4 Source Header References

CString.h
UnrealString.h
NameTypes.h

See CString.h for more details and other functions like

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

For a great deal of helpful functions you will also want to look at

UnrealString.h for direct manipulation of FStrings!

For more info on FNames check out

NameTypes.h

FArrayReaderPtr to FString

uint8 data[512];  
FMemory::Memzero(data, 512);  
  
FMemory::Memcpy(data, ArrayReaderPtr->GetData(), ArrayReaderPtr->Num());  
FString str = ((const char*)data);  

Array<uint8> to FString

TArray<uint8> 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);  

FString to char *  TCHAR_TO_ANSI()

int BP_GetColumnIndex(int resultSet, FString columnName)  
{  
    return GetColumnIndex(resultSet, TCHAR_TO_ANSI(*columnName));  
}  
    
int GetColumnIndex(int iResult, const char* columnName)  
{  
}  

FString to TCHAR *

int BP_GetColumnIndex(int resultSet, FString columnName)  
{  
    return GetColumnIndex(resultSet, *columnName);  
}  
    
int GetColumnIndex(int iResult, const TCHAR* columnName)  
{  
}  

FString To Array<uint8>

// 简洁用法
FString JsonStr;  
TArray<uint8> 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<uint8> URLData;  
URLData.SetNum(CTXSize);  
memcpy(URLData.GetData(), UTF8String.Get(), CTXSize);  

FString To FText

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

FText To FString

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

 

posted on 2018-03-24 21:42  一粒沙  阅读(4432)  评论(0编辑  收藏  举报