【UE4C++基础知识】3FString类型转换操作FString字符串转换
FString 转其他类型
FString → FNames
FString TheString = "UE4_C++_IS_Awesome";
FName ConvertedFString = FName(*TheString);
FString → Integer
FString TheString = "123.021";
int32 MyShinyNewInt = FCString::Atoi(*TheString);
FString → Float
FString TheString = "123.021";
int32 MyShinyNewInt = FCString::Atof(*TheString);
FString → char *
///1
FString str
string t = TCHAR_TO_UTF8(*str);
char * returnvalue = (char *)malloc(sizeof(char) * (t.length() + 1));
strncpy_s(returnvalue, t.length() , t.c_str(), t.length());
///2
int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
{
return GetColumnIndex(resultSet, TCHAR_TO_ANSI(*columnName));
}
int ARPG_Database::GetColumnIndex(int iResult, const char* columnName)
{
}
FString → std::string
#include <string>
//....
FString UE4Str = "Flowers";
std::string MyStdString(TCHAR_TO_UTF8(*UE4Str));
FString → const char *
const char *plainText= TCHAR_TO_ANSI(*aes_content);
FString → TCHAR *
int ARPG_Database::BP_GetColumnIndex(int resultSet, FString columnName)
{
return GetColumnIndex(resultSet, *columnName);
}
int ARPG_Database::GetColumnIndex(int iResult, const TCHAR* columnName)
{
}
FString → FText
FString Str = TEXT("str");
FText Text = FText::FromString(Str);
FString → TArray
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
Float/Integer → FString
FString NewString = FString::FromInt(YourInt);
FString VeryCleanString = FString::SanitizeFloat(YourFloat);
FText → FString
FString Name = NameDesc->GetText().ToString();
std::string → FString
#include <string>
//....
std::string TestString = "Happy";
FString HappyString(TestString.c_str());
TArray → 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);
FArrayReaderPtr → FString
uint8 data[512];
FMemory::Memzero(data, 512);
FMemory::Memcpy(data, ArrayReaderPtr->GetData(), ArrayReaderPtr->Num());
FString str = ((const char*)data);
数值→ std::to_string
std::string to_string( long value );
std::string to_string( long long value );
std::string to_string( unsigned value );
std::string to_string( unsigned long value );
std::string to_string( unsigned long long value );
std::string to_string( float value );
std::string to_string( double value );
std::string to_string( long double value );
中⽂乱码
初始化FString导致的乱码
/乱码
FString GetStrWithChinese()
{
FString fstr="这是⼀句中⽂";
return fstr;
}
//正确使⽤
FString GetStrWithChinese()
{
FString fstr=TEXT("这是⼀句中⽂");
return fstr;
}
std::string转FString导致乱码
因为std::string可以理解为char数组,char占1个字节,⽽中⽂字符根据不同平台,⼀般占⽤2个字符,超出了存储范围,因此导致乱码.
可以使⽤std::wstring(宽字符)来存储中⽂, std::wstring.cstr() 会返回⼀个 const wchar_t* 类型,UE4中常⽤的TCHAR实际上就是wchar_t 的别名.
可以认为 std::wstring.cstr()==const wchar_t*==const TCHAR*
//乱码
std::string cstr = "中⽂技术哪家强";
FString fstr=FString(cstr.cstr());
//正确使⽤
//宽字符初始化要L开头
std::wstring cwstr = L"中⽂技术哪家强?";
//还可以使⽤wchar_t*来表⽰⼀个包含中⽂的字符串
wchar_t* wchar = L"中⽂技术哪家强?";
//wchar_t*和std::wstirng的转换
c++string类型const wchar_t* tmpwchar = cwstr.c_str();
//上⾯说了TCHAR本质就是wchar_t
TCHAR* tchar = wchar;
//std::wstring.cstr()返回的是const wchar_t*,所以这⾥使⽤const_cast将其由const wchar_t*转换成wchar_t*,也就是TCHAR* TCHAR* tchar = const_cast<wchar_t*>(cwstr.c_str());
//ue4中,可以直接使⽤TCHAR*或者const TCHAR*来初始化FString
FString fstr=tchar;
//如果其他平台传来的是带有中⽂的std::string字符串
std::string cstr="中⽂技术哪家强?";
//windows平台std::string使⽤utf-8编码格式,可以使⽤ue提供的UTF8_TO_TCHAR宏将std::string转换成ue⽀持的TCHAR
FString fstr=FString(UTF8_TO_TCHAR(cstr.cstr());
FString转std::string导致乱码
std::string中存储的数据并没有被损坏,不能正常的显⽰中⽂⽽已。可以把它转换成std::wstirng看看是否正常
//乱码
//定义⼀个包含中⽂的FString
FString fstr = TEXT("这是⼀句中⽂" );
//使⽤TCHAR_TO_UTF8将FString转换成std::string
std::string cstr = TCHAR_TO_UTF8(*fstr);
//转换
//MultiByteToWideChar和WideCharToMultiByte需要导⼊Windows.h
std::wstring StringToWString(const std::string& str)
{
//定义⼀个空的std::wstring
std::wstring wstr = L"";
/**
* CodePage:该参数决定执⾏转换时使⽤的编码格式,本机使⽤编码格式为UTF8,所以使⽤CP_UTF8.
*dwFlags:使⽤0即可
*lpMultiByteStr:要转换的字符串指针,使⽤c_str()即可获得
*cbMultiByte:要转换的字符串的长度,字节为单位,可以使⽤size()获得
*lpWideCharStr:指向接收转换后字符串的缓冲区的指针
*cchWideChar:缓冲区⼤⼩,如果为0,则返回所需要的缓冲区⼤⼩
*详见docs.microsoft/zh-cn/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar?redirectedfrom=MSDN */
//lpWideCharStr设为NULL,cchWideChar设为0,该步骤⽤于获取缓冲区⼤⼩
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), NULL, 0);
//创建wchar_t数组作为缓冲区,⽤于接收转换出来的内容,数组长度为len+1的原因是字符串需要以'\0'结尾,所以+1⽤来存储'\0'
wchar_t* wchar = new wchar_t[len + 1];
//缓冲区和所需缓冲区⼤⼩都已确定,执⾏转换
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), wchar, len);
//使⽤'\0'结尾
wchar[len] = '\0';
//缓冲区拼接到std::wstring
wstr.append(wchar);
//切记要清空缓冲区,否则内存泄漏
delete[]wchar;
return wstr;
}
std::wstring转std::string
std::wstring转std::string代码,需要使⽤WideCharToMultiByte
std::string WStringToString(const std::wstring wstr)
{
std::string str;
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL,NULL); char*buffer = new char[len + 1];
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
str.append(buffer);
delete[]buffer;
return str;
}
其他
UE4 Source Header References
CString.h
UnrealString.h
NameTypes.h
StringConv.h (TCHAR_TO_ANSI etc)
CString.h可以查到更多,如
toi64 (string to int64)
Atod (string to double precision float)
更多关于FString的操作可以到UnrealString.h到.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论