unreal游戏,unreal教学,unreal素材

超级管理员

87

帖子

0

回复

254

积分

楼主
发表于 2018-02-25 01:03:12 | 查看: 348 | 回复: 0

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


FString aa = FString::Printf(TEXT("asdfas %d %s"), 333,TEXT("asdfasdf"));

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

Enjoy!

------------------------------------------------------------------------------

FArrayReaderPtr to FString

[cpp] 

  1. uint8 data[512];  

  2. FMemory::Memzero(data, 512);  

  3.   

  4. FMemory::Memcpy(data, ArrayReaderPtr->GetData(), ArrayReaderPtr->Num());  

  5. FString str = ((const char*)data);  


Array<uint8> to FString

[cpp] 

  1. TArray<uint8> content;  

  2. ...  

  3. const std::string cstr(reinterpret_cast<const char*>(content.GetData()), content.Num());  

  4. FString frameAsFString = cstr.c_str();  

  5. UE_LOG(VRSLog, Warning, TEXT("%s"), *frameAsFString);  


FString to char *  TCHAR_TO_ANSI()

[cpp] 

  1. int BP_GetColumnIndex(int resultSet, FString columnName)  

  2. {  

  3.     return GetColumnIndex(resultSet, TCHAR_TO_ANSI(*columnName));  

  4. }  

  5.     

  6. int GetColumnIndex(int iResult, const char* columnName)  

  7. {  

  8. }  



FString to TCHAR *

[cpp] 

  1. int BP_GetColumnIndex(int resultSet, FString columnName)  

  2. {  

  3.     return GetColumnIndex(resultSet, *columnName);  

  4. }  

  5.     

  6. int GetColumnIndex(int iResult, const TCHAR* columnName)  

  7. {  

  8. }  



FString To Array<uint8>

// 简洁用法[cpp] 

  1. FString JsonStr;  

  2. TArray<uint8> content;  

  3.   

  4. content.SetNum(JsonStr.Len());  

  5. memcpy(content.GetData(), TCHAR_TO_ANSI(*JsonStr), JsonStr.Len());  



[cpp] 

  1. FString StrData;  

  2. const TCHAR* StrPtr = *StrData;  

  3. FTCHARToUTF8 UTF8String(StrPtr);  

  4. int32 CTXSize = UTF8String.Length();  

  5.   

  6. TArray<uint8> URLData;  

  7. URLData.SetNum(CTXSize);  

  8. memcpy(URLData.GetData(), UTF8String.Get(), CTXSize);  


FString To FText

[cpp] 

  1. FString Str = TEXT("str");  

  2. FText Text = FText::FromString(Str);  



FText To FString

[cpp] 

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


文章来源自:http://blog.csdn.net/chinahaerbin/article/details/51075743





您的帖子在2018-03-04 20:10:21被scouther编辑
您需要登录后才可以回帖 登录 | 立即注册

Copyright © 2017-2018 Scouther Group.All rights reserved.