UE4_最全FString字符串与各格式转换 输出
最新整理FString和各种数据格式间转换,看目录里面一定有你需要
如果觉得不错的话,点个赞和喜欢吧
一、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)
二、日志打印
1.输出字符串到output log中
1.1 最普通的三种
FString filePath
UE_LOG(LogTemp, Log, TEXT("Hello,World!"));
UE_LOG(LogTemp, Warning, TEXT("Hello,World!"));
UE_LOG(LogTemp, Error, TEXT("Hello,World!"));
1.2 %d打印int,%f打印float,%s string
前面加throttle是为了知道打印的对象
UE_LOG(LogTemp,Log,TEXT("throttle %d"), throttle);
//输出:throttle 1532
FString filePath
UE_LOG(LogTemp, Log, TEXT("%s"), *filePath);
1.3 自定义 分类的名字 的log
.h
DECLARE LOG CATEGORY EXTERN(MyLog,Log,All);
.cpp
DEFINE_LOG_CATEGORY(MyLog);
UE_LOG(MyLog,Warning,TEXT ("Success"));
MyLog:Warning:Success
2.输出字符串到屏幕上
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
3.1 FString To FNames
不可靠,因为FName不区分大小写,所以转换存在丢失
//字符串
FString TheString = "Hello,world";
//将FString转换为FName
FName ConvertedFString = FName(*TheString);
3.2 FString To FText
FString TheString = "Hello,world";
FText ConvertedFSting=FText::FromSting(TheString);
3.3 FString To Array[uint8]
//方法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);
3.4 FString To char*
FString TheString = "Hello,world";
char* MyCharPtr = TCHAR_TO_UTF8(*MyFString);
3.5 FString To const char *
const char *plainText= TCHAR_TO_ANSI(*aes_content);
3.6 FString To TCHAR*
FString s="a";
TCHAR* t=*s;
3.7 FString To std::string
#include <string>
FString UE4Str = TEXT("Flowers");
std::string MyStdString = TCHAR_TO_UTF8(*UE4Str);
3.8 FString To JsonObject
处理Json需要在.Build.cs中添加Json模块
PrivateDependencyModuleNames.AddRange(
new string[]
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"Json",
// ... add private dependencies that you statically link with here ...
);
//注意String中使用双引号
FString JsonStr = TEXT("{\"key\":\"value\",\"key2\":\"value2\"}");
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonStr);
FJsonSerializer::Deserialize(Reader,JsonObject);
//JsonObject就是Json
3.9 FString To bool
FString TheString = "123.021";
bool MyBool = MyFString.ToBool();
3.10FString To int32
FString TheString = "123.021";
//注意,Atoi和Atof是静态函数,所以使用语法FCString::TheFunction调用它:)
int32 MyShinyNewInt = FCString::Atoi(*TheString);
3.11 FString To int64
FString TheString = "12312345678789561234879";
//注意,Atoi64是静态函数,所以使用语法FCString::TheFunction调用它:)
int64 MyShinyNewInt = FCString::Atoi64(*TheString);
3.12 FString To float
FString TheString = "123.021";
//注意,Atoi和Atof是静态函数,所以使用语法FCString::TheFunction调用它:)
float MyShinyNewFloat = FCString::Atof(*TheString);
3.13 FString To double
//函数方式
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());
3.14 转换 To FString
//int
FString NewString = FString::FromInt(MyInt);
//float //double ----只能精确到小数点后6位
FString VeryCleanString = FString::SanitizeFloat(MyFloat);
//bool
FString MyFString = MyBool?TEXT("true") :TEXT("false");
//FVector
FString MyFString = MyVector.ToString();
//FVector2D
FString MyFString = MyVector2D.ToString();
//FRotator
FString MyFString = MyRotator.ToString();
//FLinearColor
FString MyFString = MyLinearColor.ToString();
//UObject
FString MyFString = (MyObj != NULL) ? MyObj->GetName() : FString(TEXT("None"));
//FMatrix
FMatrix rotationMatrix(FPlane(0,1,2,3),FPlane(4,5,6,7),FPlane(8,9,10,11),FPlane(12,13,14,15));
FString str = rotationMatrix.ToString();
3.15 double To FString
//直接转换:精确到小数点后6位
return FString::SanitizeFloat(numeral.dou);
//double→string→FString精确到小数点后6位
std::string Str = std::to_string(numeral.dou);
FString string = UTF8_TO_TCHAR(Str.c_str());
//输出有效位数15位
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
string str;
double d = numeral.dou;
stringstream ss;
ss << setprecision(15) << d;
str = ss.str();
FString string = UTF8_TO_TCHAR(str.c_str());
//指定小数点后几位 --注:double的有效位数只有16位,小数点算一位
double doub = 9999.1234567891011121314;
FString TimeDesc = FString::Printf(TEXT("%.15f"), doub); //9999.123456789100601
3.16 bytes To FString
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);
3.17 FArrayReaderPtr to FString
uint8 data[512];
FMemory::Memzero(data, 512);
FMemory::Memcpy(data, ArrayReaderPtr->GetData(), ArrayReaderPtr->Num());
FString str = ((const char*)data);
3.18 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);
3.19 FString To MD5(FString)
FString str = TEXT("someStuff");
FString md5 = FMD5::HashAnsiString();
3.20 FString To Ascii/Unicode
FString str = TEXT("123苹果");
int32 ascii = (int32)Source[4]; //数字代表序号
int32 ascii2 = (int32)Source.GetCharArray()[4]; //或者第二种写法
3.21 FString From Ascii /Unicode
FString str;
int32 ascii = 49; //1的ascii是49。 中文 中的编码是20013
str.AppendChar(Ascii); //如果ascii为20013,转换出来时中
四、FName
4.1 FName To FString
FName str = FName(TEXT("hello"));
FString fstr = str.ToString();
4.2 FName To FText
FName str = FName(TEXT("hello"));
FText = FText::FromName(str);
五、FText
FText To FString
//不可靠,在一些语言中存在损耗
FString Name = MyText.ToString();
六、TChar*
TChar* 与 char* 的互相转换
主要是使用下面的四个宏定义。
TCHAR_TO_ANSI(*str)
ANSI_TO_TCHAR(*str)
TCHAR_TO_UTF8(*str)
UTF8_TO_TCHAR(*str)
七、std::string
std::string To FString
#include <string>
std::string MyStdString = "Happy";
FString HappyString(UTF8_TO_TCHAR(MyStdString.c_str())); //可防止string的中文转换出错
To string函数
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
std::string str = "123";
int n = atoi(str.c_str());
cout<<n; //123
double->string
//有效位数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
string str="1245683.141592653";
double num=atof(str.c_str());
printf("%0.9lf\n",num);
八、char*
char* To FString
FString MyFString = FString(UTF8_TO_TCHAR(MyCharPtr));
九、UE4_Json
JsonObject To FString
处理Json需要在.Build.cs中添加Json模块
PrivateDependencyModuleNames.AddRange(
new string[]
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
"Json",
// ... add private dependencies that you statically link with here ...