如何将一串字符在Unicode和UTF8之间互相转换?最好有源码,能讲解原理也行。
解决方案 »
- 网上一个例子//p_Paint****都是什么意思?
- opengl es 1.x sdk PC Emulation不能在mfc工程中运行吗
- 这句查询语言错在哪了?
- 如何用VC使EXCEL形成固定表头
- 我抄的书上的MFC程序,运行后报IDC_MYCOMBOBOX未定义,IDC_ 主要是什么变量使用的,我该在哪定义。
- 床烦一下各位!关于具条的下拉箭头!!
- 怎样改变最近打开文件列表中文件的数目
- 100分,谁要
- 问一个C++的问题
- 那位高手能谈谈vc中setting菜单里各个参数的设置和意义
- 请问回掉函数市什么意思?
- 急求解答!在VC中嵌入WORD后如何打开word文档和保存成word可以打开的文档?
LdapUTF8ToUnicodeHeader: Declared in Winldap.h.
Library: Use Wldap32.lib.
MultiByteToWideChar()/WideCharToMultiByte(), using CP_UTF8 for the
CodePage parameter,
utf8ToUnicode() is a function that converts a utf8 string (char
array)
to a UNICODE string (wchar_t array) based on the utf8 specification
MultiByteToWideChar()/WideCharToMultiByte(), using CP_UTF8 for the
CodePage parameter,
utf8ToUnicode() is a function that converts a utf8 string (char
array)
to a UNICODE string (wchar_t array) based on the utf8 specification
CString szText = "中华人民共和国";int nLen = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, (LPCTSTR)szText, szText.GetLength(), NULL, 0);
//此处获得的nLen为什么老是等于0 ??
if(nLen > 0){
LPWSTR m_pUTF8 = new WCHAR[nLen]; // 分配内存
MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, (LPCTSTR)m_strListing, m_strListing.GetLength(), m_pUTF8, nLen);
}
memset(pszText, 0, 128);
strcpy(pszText, "中华人民共和国");
//此名如果strcpy(pszText, "abcdefg");则可以还原
int nUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, pszText, -1, NULL, 0);
wchar_t *pUTF8=NULL; if(nUnicodeLen > 0){
pUTF8 = (wchar_t*)malloc(sizeof(wchar_t)*(nUnicodeLen+1)); MultiByteToWideChar(CP_UTF8, 0, pszText, strlen(pszText), pUTF8, nUnicodeLen);
} char *pszEncode=new char[128];
memset(pszEncode, 0, 128);
WideCharToMultiByte(CP_UTF8, 0, pUTF8, nUnicodeLen, pszEncode, 128, NULL, NULL);
//如果最上面的pszText中是中文字符的话,pszEncode中解出来的是乱七八糟的东西,如果是英文数字则没有问题。 delete []pszText;pszText=NULL;
delete []pszEncode;pszEncode=NULL;