码迷,mamicode.com
首页 > 其他好文 > 详细

Unicode与Ansi互转

时间:2017-07-14 23:55:47      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:wchar   手动   需要   函数返回   cst   blog   bool   turn   res   

 1 BOOL CTool::AnsiToUnicode(const char *pSrc, CString &strResult)
 2 {
 3 #ifndef _UNICODE
 4     return FALSE;
 5 #endif
 6     int nLen=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pSrc,-1,NULL,0);
 7     strResult.Empty();
 8     if(nLen==0)
 9         return FALSE;
10     wchar_t* pResult=new wchar_t[nLen];
11     MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pSrc,-1,pResult,nLen);
12     strResult.Format(_T("%s"),pResult);
13     delete[] pResult;
14     pResult=NULL;
15     return TRUE;
16 }
17 
18 wchar_t* CTool::AnsiToUnicode(const char *szStr)
19 {
20     int nLen=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,szStr,-1,NULL,0);
21     if(nLen==0)
22         return NULL;
23     wchar_t* pResult=new wchar_t[nLen];
24     MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,szStr,-1,pResult,nLen);
25     return pResult;
26 }
27 
28 char* CTool::UnicodeToAnsi(const wchar_t *szStr)
29 {
30     int nLen=WideCharToMultiByte(CP_ACP,0,szStr,-1,NULL,0,NULL,NULL);
31     if(nLen==0)
32         return NULL;
33     char* pResult=new char[nLen];
34     WideCharToMultiByte(CP_ACP,0,szStr,-1,pResult,nLen,NULL,NULL);
35     return pResult;
36 }

后面两个函数返回一个new的指针,接收用完后需要自己手动释放

Unicode与Ansi互转

标签:wchar   手动   需要   函数返回   cst   blog   bool   turn   res   

原文地址:http://www.cnblogs.com/xslwm/p/7173091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!