文章

FString相关

FString相关

UE4 Source Header References

1
2
3
4
#include "CString.h" 
#include "UnrealString.h"
#include "NameTypes.h"
#include "StringConv.h" //(TCHAR_TO_ANSI etc)

CString.h可以查到更多,如

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

日志打印

输出字符串到output log中

1
2
3
4
5
6
7
8
DECLARE_LOG_CATEGORY_EXTERN(MyLog, Log, All);//.h
DEFINE_LOG_CATEGORY(MyLog);//.Cpp


FString filePath
UE_LOG(MyLog, Log, TEXT("Hello,World!"));
UE_LOG(MyLog, Warning, TEXT("Hello,World!"));
UE_LOG(MyLog, Error, TEXT("Hello,World!"));

字符串格式符

  • %d - int
  • %f = float
  • %s = string
1
2
3
4
UE_LOG(LogTempLogTEXT("throttle %d"), throttle);
//输出:throttle 1532
FString filePath
UE_LOG(LogTemp, Log, TEXT("%s"), *filePath);

自定义 分类的名字 的log

1
2
3
4
5
6
7
8
9
10
.h
    DECLARE LOG CATEGORY EXTERN(MyLog,LogAll);

.c++
    DEFINE_LOG_CATEGORY(MyLog);

UE_LOG(MyLog,Warning,TEXT ("Success"));

输出:
MyLog:Warning:Success

输出字符串到屏幕上

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

FString To

To FNames

不可靠,因为FName不区分大小写,所以转换存在丢失

1
2
3
4
//字符串
FString TheString = "Hello,world";
//将FString转换为FName
FName ConvertedFString = FName(*TheString);

To FText

1
2
FString TheString = "Hello,world";
FText ConvertedFSting=FText::FromSting(TheString);

To Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//方法1
FString JsonStr;  
TArray<uint8> content;  
 
content.SetNum(JsonStr.Len());  
memcpy(content.GetData(), TCHAR_TO_ANSI(*JsonStr), JsonStr.Len());

//方法2
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);

To char*

1
2
FString TheString = "Hello,world";
char* MyCharPtr = TCHAR_TO_UTF8(*MyFString);

To const char *

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

To TCHAR*

1
2
FString s="a";
TCHAR* t=*s;

To std::string

1
2
3
4
#include <string>

FString UE4Str = "Flowers";
std::string MyStdString = TCHAR_TO_UTF8(*UE4Str);

To JsonObject

1
2
3
4
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonStr);
FJsonSerializer::Deserialize(Reader,JsonObject);
//JsonObject就是Json

To bool

1
2
FString TheString = "123.021";
bool MyBool = MyFString.ToBool();

To int32

1
2
3
FString TheString = "123.021";
//注意,Atoi和Atof是静态函数,所以使用语法FCString::TheFunction调用它:)
int32 MyShinyNewInt = FCString::Atoi(*TheString);

To int64

1
2
3
FString TheString = "12312345678789561234879";
//注意,Atoi64是静态函数,所以使用语法FCString::TheFunction调用它:)
int64 MyShinyNewInt = FCString::Atoi64(*TheString);

To float

1
2
3
FString TheString = "123.021";
//注意,Atoi和Atof是静态函数,所以使用语法FCString::TheFunction调用它:)
float MyShinyNewFloat = FCString::Atof(*TheString);

To double

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//函数方式
bool UDoubleFunction::FStringToDouble(FString str,double &num)
{
    std::string numString = TCHAR_TO_UTF8(*str);
    double b = atof(numString.c_str());
    if (b == NULL)
    {
        return false;
    }
    num =b;
    return true;
}

//直接转换
FString TheString = "123.021";
//注意,Atod是静态函数,所以使用语法FCString::TheFunction调用它:)
double MyShinyNewFloat = FCString::Atod(*TheString);

//FString转String转Double
FString str = "113.5246854";
std::string numString = TCHAR_TO_UTF8(*str);
double b = atof(numString.c_str());

To FText

1
2
FString tempInfo = TEXT("Halo");
FText = FText::FromString(tempInfo);

To FString

FText To

1
2
//不可靠,在一些语言中存在损耗
FString Name = MyText.ToString();  

bytes To

1
2
3
4
5
6
7
8
FString fstr;
for (int i(0); i < FaceArray.Num() * 4; ++i)
{
    FString tmpStr;
    ByteToHex(f_arrbytes[i], tmpStr);
    fstr.Append(tmpStr);
}
UE_LOG(LogTemp, Log, TEXT("%S"), *fstr);

FArrayReaderPtr to

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

Array to

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

std::string To

1
2
3
4
#include <string>

std::string MyStdString = "Happy"; 
FString HappyString(UTF8_TO_TCHAR(MyStdString.c_str()));  //可防止string的中文转换出错

TCHAR* To

1
FString MyFString = FString(UTF8_TO_TCHAR(MyCharPtr));

FName

To FString

1
2
FName str = FName(TEXT("hello"));
FString fstr = str.ToString();

To FText

1
2
FName str = FName(TEXT("hello"));
FText = FText::FromName(str);

TChar*

TChar* 与 char* 的互相转换

主要是使用下面的四个宏定义。

  • TCHAR_TO_ANSI(str)
  • ANSI_TO_TCHAR(str)
  • TCHAR_TO_UTF8(str)
  • UTF8_TO_TCHAR(str)

std::string

To string函数

1
2
3
4
5
6
7
8
9
10
//c++11标准增加了全局函数std::to_string:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);       //只能精确到小数点后6位
string to_string (double val);      //只能精确到小数点后6位
string to_string (long double val);

string To函数

string To int

1
2
3
std::string str = "123";
int n = atoi(str.c_str());
cout<<n; //123

double->string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//有效位数20位   ---据说待考证
char buffer[100];
double b=1245683.141592653;
int ndight=20;//有效数字位数
gcvt(b,ndight,buffer);
cout<<buffer<<endl;

//一般使用方法:只能精确到小数点后6位
#include <string>
using namespace std;
double d = 3.1415926535897932384;
string str = std::to_string(d);
cout << str << std::endl; // 3.141593

//使用stringstream,在输入流时使用setprecision设置精度,需包含头文件 <iomanip>
//精度保持到小数点后15位
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
std::stringstream ss;
ss << setprecision(15) << d;
str = ss.str();   
cout << str << std::endl; // 3.14159265358979

//对于double类型,setprecision(15) 参数最大有效值为15,超过15,数据就不保证可靠了。

string->double

1
2
3
string str="1245683.141592653";
double num=atof(str.c_str());
printf("%0.9lf\n",num);

UE4_Json

JsonObject To FString

1
2
3
4
//Jdata转换为了JsonStr
FString JsonStr;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&JsonStr);
FJsonSerializer::Serialize(Jdata.ToSharedRef(), Writer);

Utils

FString::Join

1
2
3
4
TArray<FString> tempVerions;
tempVerions.Add(curAppVersion);
tempVerions.Add(curResVersion);
FString tempSaveVal = FString::Join(tempVerions, *SplitFlag);

格式化

左侧补0

1
 FString::Printf(TEXT("%09d"), InID);
本文由作者按照 CC BY 4.0 进行授权