背景
- 网络传输内容, 但是是GB2312编码, 而UE需要UTF-8, UTF-16编码。
解决方案
- 内容GB2312->转码 UTF-8
- 实现测试 源码 如下: (读取本地文件指定编码文件[GB2312/ANSI],解码, 转FString)
UFUNCTION(BlueprintCallable)
static bool ReadRile(const FString& InFilePath, FString& OutContent);
static FString ANSITOUTF_8(const char* ANSI_Str);
/* Win .h*/
#if _WIN32
#include "Windows/AllowWindowsPlatformTypes.h"
#include "stringapiset.h"
#include "Windows/HideWindowsPlatformTypes.h"
#endif
bool UVideoUploadBPLibrary::ReadRile(const FString& InFilePath, FString& OutContent)
{
FString FilePath = InFilePath;
//FilePath = TEXT("E:\\workspace\\py\\Temp\\test4.txt");
// 获取平台文件对象
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
// 打开文件并读取内容
TUniquePtr<IFileHandle> FileHandle(PlatformFile.OpenRead(*FilePath));
if (!FileHandle)
{
UE_LOG(LogTemp, Error, TEXT("Failed to open file: %s"), *FilePath);
return false;
}
// 读取文件内容
const int64 FileSize = FileHandle->Size();
uint8* CacheBuffer = new uint8[FileSize];
FileHandle->Read(CacheBuffer, FileSize);
// 转换数据到指定编码
OutContent = ANSITOUTF_8((char*)CacheBuffer);
UE_LOG(LogTemp, Error, TEXT("OutContent: %s"), *OutContent);
return true;
}
FString UVideoUploadBPLibrary::ANSITOUTF_8(const char* ANSI_Str)
{
wchar_t* wideCharString;
FString Res;
int wideCharCount = MultiByteToWideChar(CP_ACP, 0, ANSI_Str, -1, NULL, 0);
wideCharString = new wchar_t[wideCharCount];
MultiByteToWideChar(CP_ACP, 0, ANSI_Str, -1, wideCharString, wideCharCount);
Res = FString(wideCharString);
delete (wideCharString);
return Res;
}