UTF8 Conversion
I need to generate a开发者_开发百科 UTF8 string to pass to a 3rd party library and I'm having trouble figuring out the right gymnastics... Also, to make matters worst, I'm stuck using C++ Builder 6 and every example I found talks about using std::string which CBuilder6 evidentially has no support for. I'd like to accomplish this without using STL what so ever.
Here is my code so far that I can't seem to make work.
wchar_t *SS1;
char *SS2;
SS1 = L"select * from mnemonics;";
int strsize = WideCharToMultiByte(CP_UTF8, 0, SS1, wcslen(SS1), NULL, 0, NULL, NULL);
SS2 = new char[strsize+1];
WideCharToMultiByte( CP_UTF8, 0, SS1, wcslen(SS1), SS2, strsize, NULL, NULL);
3rd party library chokes when I pass it SS2 as a parameter. Obviously, I'm on a Windows platform using Microsoft's WideCharToMultiByte but eventually I would like to not need this function call as this code must also be compiled on an embedded platform as well under Linux but I'll cross that bridge when I get to it.
For now, I just need to be able to either convert a wchar_t or char to UTF8 encoded string preferably without using any STL. I won't have STL on the embedded platform.
Thanks!
Something like that:
extern void someFunctionThatAcceptsUTF8(const char* utf8);
const char* ss1 = "string in system default multibyte encoding";
someFunctionThatAcceptsUTF8( w2u( a2w(ss1) ) ); // that conversion you need:
// a2w: "ansi" -> widechar string
// w2u: widechar string -> utf8 string.
You just need to grab and include this file: http://code.google.com/p/tiscript/source/browse/trunk/sdk/include/aux-cvt.h
It should work on Builder just fine.
If you're still looking for an answer, here's a simple implementation of a utf8 convertor in C language:
/*
** Transforms a wchar to utf-8, returning a string of converted bytes
*/
void ft_to_utf8(wchar_t c, unsigned char *buffer)
{
if (c < (1 << 7))
*buffer++ = (unsigned char)(c);
else if (c < (1 << 11))
{
*buffer++ = (unsigned char)((c >> 6) | 0xC0);
*buffer++ = (unsigned char)((c & 0x3F) | 0x80);
}
else if (c < (1 << 16))
{
*buffer++ = (unsigned char)((c >> 12) | 0xE0);
*buffer++ = (unsigned char)(((c >> 6) & 0x3F) | 0x80);
*buffer++ = (unsigned char)((c & 0x3F) | 0x80);
}
else if (c < (1 << 21))
{
*buffer++ = (unsigned char)((c >> 18) | 0xF0);
*buffer++ = (unsigned char)(((c >> 12) & 0x3F) | 0x80);
*buffer++ = (unsigned char)(((c >> 6) & 0x3F) | 0x80);
*buffer++ = (unsigned char)((c & 0x3F) | 0x80);
}
*buffer = '\0';
}
精彩评论