调用MultiByteToWideChar从UTF-8转换到Unicode编码的时候,为什么每次运行得到的结果不同呢?求解释解决方案
调用MultiByteToWideChar从UTF-8转换到Unicode编码的时候,为什么每次运行得到的结果不同呢?求解释
如题,在做一个Clucene的搜索,涉及到中文编码的问题,但是在转换编码的时候出现了问题,希望知道的人给个答案。
------解决方案--------------------
MultiByteToWideChar
我只做过gb2312转换为 unicode的。。
utf-8能转吗?
------解决方案--------------------
MultiByteToWideChar(CP_UTF8,...);
注意:The lpMultiByteStr and lpWideCharStr pointers must not be the same.
------解决方案--------------------
当然可以,先从GB2312转换为Unicode编码,再从unicode编码转换为国际通用的utf-8编码
转换函数如下:
如题,在做一个Clucene的搜索,涉及到中文编码的问题,但是在转换编码的时候出现了问题,希望知道的人给个答案。
------解决方案--------------------
MultiByteToWideChar
我只做过gb2312转换为 unicode的。。
utf-8能转吗?
------解决方案--------------------
MultiByteToWideChar(CP_UTF8,...);
注意:The lpMultiByteStr and lpWideCharStr pointers must not be the same.
------解决方案--------------------
当然可以,先从GB2312转换为Unicode编码,再从unicode编码转换为国际通用的utf-8编码
转换函数如下:
- C/C++ code
#include <string> #include <windows.h> #include <iostream> using std::string; using std::wstring; using std::cout; using std::endl; //gb2312 to Unicode wstring GB2312ToUnicode(constr sting &str) { int len = ::MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0); wchar_t *rs = new wchar_t[len + 1]; memset(rs,0,(len + 1)*sizeof(wchar_t)); ::MultiByteToWideChar(CP_ACP,0,str.c_str(),static_cast<LPWSTR>(rs),len); wstring ws = static_cast<wchar_t *>(rs); delete []rs; return ws; } //Unicode to UTF-8 string UnicodeToUTF8( const wstring& str ) { char* pElementText; int iTextLen = WideCharToMultiByte( CP_UTF8,0,str.c_str(),-1,NULL,0,NULL, NULL ); pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) ); ::WideCharToMultiByte( CP_UTF8,0,str.c_str(),-1,pElementText,iTextLen,NULL, NULL ); string strText; strText = pElementText; delete[] pElementText; return strText; }
------解决方案--------------------
TCHAR tf_keyword[CL_MAX_DIR] = {0};
MultiByteToWideChar(936,0,query.m_strFilmType.c_str(),-1,tf_keyword,CL_MAX_DIR);
std::cout<<tf_keyword<<std::endl;
你的代码中936是什么意思啊,multibytetowidechar函数的第一个参数代表是你当前系统所用的字符编码,如果你MultiByteToWideChar(CP_UTF8,...)就说明你当前用的字符编码就是utf-8,你还再转换到utf-8有什么意思啊
------解决方案--------------------
你应该看看你当前系统用的是什么编码,查看你工程的属性,
The lpMultiByteStr and lpWideCharStr pointers must not be the same
这两个参数的类型是:lpmultibytesstr的类型是:LPCSTR,也就是const char *,
lpwidecharstr的类型是: LPWSTR,也就是:wchar_t *