当前位置: 首页 > 工具软件 > LuaBind > 使用案例 >

luabind 导出string问题

牧梓
2023-12-01

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;
}

转载于:https://www.cnblogs.com/rexzhao/p/4598182.html

 类似资料: