String Encoding Throwing errors at Certain Characters

Hi,

I am using a few string encoding to convert from FString to Char* and vice versa. I am using this because of one of my plugins, MSSQL integration, which uses a custom C# based library to connect to SQL Server from Unreal , and pass on query strings.

I am using the below functions :

FString GetFStringfromChar(const char* Input)
{
	const size_t length = 1 + strlen(Input);
	wchar_t* wcsText = new wchar_t[length];

	size_t convertedSize = 0;
	mbstowcs_s(&convertedSize, wcsText, length, Input, _TRUNCATE);

	FString Result =  FString(wcsText);
	delete[](wcsText);

	return Result;
}

char* GetCharfromFString(FString Query)
{
	const TCHAR* queryTChar = *Query;
	size_t len;
	wcstombs_s(&len, nullptr, 0, queryTChar, 0);

	size_t convertedSize = 0;
	char *charBuffer = static_cast<char*>(malloc(len));
	wcstombs_s(&convertedSize, charBuffer, len,
		queryTChar, _TRUNCATE);

	return charBuffer;
}

This works with most characters and languages, but fails at some including the special character € where it throws invalid character error while converting from FString, and with Chinese characters this updates the server with gibberish characters.

I also tried with Chinese localization to make this work with Chinese, but then it does not work for German characters.

I am wondering if there is a way to properly encode and decode the special characters and languages from all countries.

Thanks.

1 Like