【转】wchar_t*和char*之间的互相转换

Posted on 2009-10-06 14:15  一有新人  阅读(1715)  评论(0编辑  收藏  举报
 1 //将单字节char*转化为宽字节wchar_t*
 2 inline wchar_t* AnsiToUnicode( const char* szStr )
 3 {
 4     int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
 5     if (nLen == 0)
 6     {
 7         return NULL;
 8     }
 9     wchar_t* pResult = new wchar_t[nLen];
10     MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
11     return pResult;
12 }
13 //将宽字节wchar_t*转化为单字节char*
14 inline char* UnicodeToAnsi( const wchar_t* szStr )
15 {
16     int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
17     if (nLen == 0)
18     {
19         return NULL;
20     }
21     char* pResult = new char[nLen];
22     WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
23     return pResult;
24 }
留着备用

Copyright © 2024 一有新人
Powered by .NET 8.0 on Kubernetes