问一下这里为什么长度乘以2呀?memset(szUtf8,0,len * 2 + 2);Unicode到UTF8转换char* UnicodeToUtf8(CString unicode){int len; len = WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)unicode,-1,NULL,0,NULL,NULL); char *szUtf8=new char[len + 1];memset(s
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/26 15:56:17
问一下这里为什么长度乘以2呀?memset(szUtf8,0,len * 2 + 2);Unicode到UTF8转换char* UnicodeToUtf8(CString unicode){int len; len = WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)unicode,-1,NULL,0,NULL,NULL); char *szUtf8=new char[len + 1];memset(s
问一下这里为什么长度乘以2呀?memset(szUtf8,0,len * 2 + 2);
Unicode到UTF8转换
char* UnicodeToUtf8(CString unicode)
{
int len;
len = WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)unicode,-1,NULL,0,NULL,NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8,0,len * 2 + 2);
WideCharToMultiByte (CP_UTF8,0,(LPCWSTR)unicode,-1,szUtf8,len,NULL,NULL);
return wszUtf8;
}
问一下这里为什么长度乘以2呀?memset(szUtf8,0,len * 2 + 2);Unicode到UTF8转换char* UnicodeToUtf8(CString unicode){int len; len = WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)unicode,-1,NULL,0,NULL,NULL); char *szUtf8=new char[len + 1];memset(s
我觉得你这个程序本身可能存在问题.
char *szUtf8=new char[len + 1]; //这个申请的是len+1个char的内存变量
memset(szUtf8,0,len * 2 + 2);//这里要操作的内存空间为 (len+1)*2个的char空间,这个肯定是内存溢出,程序出现未知异常.
我在网上看到过相关的例程,给你参考下
void ConvertGBKToUtf8(char* rtnBuffer,const char* strGBK)
{
int len=MultiByteToWideChar(CP_ACP,0,(LPCTSTR)strGBK,-1,NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1]; //他这里很明白,空间足够
memset(wszUtf8,0,len * 2 + 2);//对这个来说
MultiByteToWideChar(CP_ACP,0,(LPCTSTR)strGBK,-1,(LPWSTR)wszUtf8,len);
len = WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)wszUtf8,-1,NULL,0,NULL,NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8,0,len + 1);
WideCharToMultiByte (CP_UTF8,0,(LPCWSTR)wszUtf8,-1,szUtf8,len,NULL,NULL);
strcpy(rtnBuffer,szUtf8);
delete[] szUtf8;
delete[] wszUtf8;
}