文章出處

luabind導出字符串 不能導出char* 會有問題 應該是字符串連接的時候出錯了

static _TCHAR* pRetChar = new _TCHAR[10];
memcpy(pRetChar,szName,10);
return NetE::wtoutf8(pRetChar);   //WCharToMultiByte 為什么就不可以那?

std::string wtoutf8 ( const wchar_t *wstr )
{
std::string str = "";
if ( NULL == wstr ) return str;

size_t wlen = wcslen(wstr);
if ( wlen >= 1020 ) wlen = 1020; // 防止溢出

unsigned char obuff[1024] = "";

unsigned char *t = obuff;
wchar_t *us = const_cast<wchar_t*>(wstr);

 

int UNICODE_CHARS = (sizeof(wchar_t) >= 4 ? 0x110000 : 0x10000);

for (size_t i = 0; i < wlen; i++)
{
wchar_t mychar = us[i];
if (mychar <= 0x7F)
{
*t++ = mychar; /* 7 sig bits */
}
else if (mychar <= 0x7FF)
{ /* 11 sig bits */
*t++ = 0xC0 | (unsigned char) (mychar >> 6); /* upper 5 bits */
*t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lower 6 bits */
}
else if (mychar <= 0xFFFF)
{ /* 16 sig bits */
*t++ = 0xE0 | (unsigned char) (mychar >> 12); /* upper 4 bits */
*t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
*t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lowest 6 bits */
}
else if (mychar < UNICODE_CHARS)
{ /* 21 sig bits */
*t++ = 0xF0 | (unsigned char) ((mychar >> 18) & 0x07); /* upper 3 bits */
*t++ = 0x80 | (unsigned char) ((mychar >> 12) & 0x3F); /* next 6 bits */
*t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
*t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lowest 6 bits */
}
}
str = (char*)obuff;
return str;
}


文章列表


不含病毒。www.avast.com
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()