luabind 导出string问题

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值