UnrealEngine GB2312->转码 UTF-8

背景

  • 网络传输内容, 但是是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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值