一、UE4 Source Header References
1 | CString.h |
CString.h可以查到更多,如1
2atoi64 (string to int64)
Atod (string to double precision float)
二、日志打印
1.输出字符串到output log中
1.1 最普通的三种
1 | FString filePath |
前面加throttle是为了知道打印的对象1
2
3
4
5
6
UE_LOG(LogTemp,Log,TEXT("throttle %d"), throttle);
//输出:throttle 1532
FString filePath
UE_LOG(LogTemp, Log, TEXT("%s"), *filePath);
1.3 自定义 分类的名字 的log
1 | .h |
2.输出字符串到屏幕上
1 | FString filePath; |
三、FString
3.1 FString To FNames
不可靠,因为FName不区分大小写,所以转换存在丢失1
2
3
4//字符串
FString TheString = "Hello,world";
//将FString转换为FName
FName ConvertedFString = FName(*TheString);
3.2 FString To FText
1 | FString TheString = "Hello,world"; |
3.3 FString To Array
1 | //方法1 |
3.4 FString To char*
1 | FString TheString = "Hello,world"; |
3.5 FString To const char *
1 | const char *plainText= TCHAR_TO_ANSI(*aes_content); |
3.6 FString To TCHAR*
1 | FString s="a"; |
3.7 FString To std::string
1 |
|
3.8 FString To JsonObject
1 | TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject); |
3.9 FString To bool
1 | FString TheString = "123.021"; |
3.10 FString To int32
1 | FString TheString = "123.021"; |
3.11 FString To int64
1 | FString TheString = "12312345678789561234879"; |
3.12 FString To float
1 | FString TheString = "123.021"; |
3.13 FString To double
1 | //函数方式 |
3.14 转换 To FString
1 | //int |
3.15 double To FString
1 | //直接转换:精确到小数点后6位 |
3.16 bytes To FString
1 | FString fstr; |
3.17 FArrayReaderPtr to FString
1 | uint8 data[512]; |
FString str = ((const char*)data);
3.18 Array to FString
1 | TArray<uint8> content; |
四、FName
4.1 FName To FString
1 | FName str = FName(TEXT("hello")); |
4.2 FName To FText
1 | FName str = FName(TEXT("hello")); |
五、FText
FText To FString1
2//不可靠,在一些语言中存在损耗
FString Name = MyText.ToString();
六、TChar*
TChar 与 char 的互相转换
主要是使用下面的四个宏定义。1
TCHAR_TO_ANSI(str) ANSI_TO_TCHAR(str) TCHAR_TO_UTF8(str) UTF8_TO_TCHAR(str)
七、std::string
1 | std::string To FString |
std::string MyStdString = “Happy”;
FString HappyString(UTF8_TO_TCHAR(MyStdString.c_str())); //可防止string的中文转换出错
To string函数1
2
3
4
5
6
7
8
9
10c++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 int1
2
3std::string str = "123";
int n = atoi(str.c_str());
cout<<n; //123
double->string1
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//有效位数20位 ---据说待考证
char buffer[100];
double b=1245683.141592653;
int ndight=20;//有效数字位数
gcvt(b,ndight,buffer);
cout<<buffer<<endl;
//一般使用方法:只能精确到小数点后6位
using namespace std;
double d = 3.1415926535897932384;
string str = std::to_string(d);
cout << str << std::endl; // 3.141593
//使用stringstream,在输入流时使用setprecision设置精度,需包含头文件 <iomanip>
//精度保持到小数点后15位
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->double1
2
3string str="1245683.141592653";
double num=atof(str.c_str());
printf("%0.9lf\n",num);
八、char*
char* To FString1
FString MyFString = FString(UTF8_TO_TCHAR(MyCharPtr));
九、UE4_Json
JsonObject To FString1
2
3
4//Jdata转换为了JsonStr
FString JsonStr;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&JsonStr);
FJsonSerializer::Serialize(Jdata.ToSharedRef(), Writer);