wchar_t*和char*之间的互相转换 (转)
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 }
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 }