Game Development Community

simple MBCS to UTF8 converting method

by game4Rest · 07/17/2009 (2:39 am) · 2 comments

When we connect TGEA to another server which is not part of Torque,
if the server supports MBCS(Multi-Byte Character Set), not unicode,
strings of some language will not be displayed correctly.

It will be the same when we receives DB data from DBMSs such as mySql.
To solve this problem, we need to convert those MBCS to unicode.
And here is a simple but useful converting method.

I choose to add this into tcpObject.cpp.
//Convert MBCS to unicode
UTF8* convertMBCStoUnicode(char* mutibyeStr) {
	int nSize=0;
	WCHAR* uniStr;
	nSize=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mutibyeStr, -1, NULL, 0);
	uniStr=(PWCHAR)calloc(nSize+1, sizeof(WCHAR));
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mutibyeStr, -1, uniStr, nSize);
	
        //to UTF8
        UTF8* ret = convertUTF16toUTF8(uniStr);
	return ret;
}

To expose it to script, add this.
//added for tcpConnection unicode support
ConsoleFunction(convertMBCStoUnicode, const char*, 2,2,"Convert MBCS to Unicode")
{
	argc;
	UTF8* ret = convertMBCStoUnicode((char*)argv[1]);
	return ret;
}

Now when you receive strings from the alien server, you can call above function like this.
%someStringFromTheAlienSvr;
%TorqueFriendlyString =convertMBCStoUnicode( someStringFromTheAlienSvr);

I just tested it with TGEA 1.8.1.

Enjoy!



#1
07/21/2009 (9:55 pm)
Great help for my work.. thanks..

There are some memory leak in the code..

how about following code to prevent the leak?
--------------------------------------------------
//Convert MBCS to unicode
UTF8* convertMBCStoUnicode(char* mutibyeStr) {
int nSize=0;
WCHAR* uniStr;
nSize=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mutibyeStr, -1, NULL, 0);
uniStr=(PWCHAR)calloc(nSize+1, sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mutibyeStr, -1, uniStr, nSize);

//to UTF8
UTF8* ret = convertUTF16toUTF8(uniStr);

StringTableEntry result = StringTable->insert( ret );

free(uniStr); uniStr = NULL;
SAFE_DELETE_ARRAY( ret );

return const_cast<char*>( result );
}
--------------------------------------------------
#2
07/22/2009 (2:54 am)
@SpaceIllusion,

Thanks so much for your update. I am so pleased that someone is trying this and comes out with better one. By the way, have you found the well working chatting system for our language. If not, you'll find it in the
Naver cafe.

Good Luck!