将string|char*转换为wstring|wchar_t*的例子:
1、string转换为wstring:
string str=_T("翔翔糖糖");
int size=MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0);
wchar_t *ch=new wchar_t[size+1];
if(!MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,ch,size))
{ return false;}

wstring wstr=ch;
2、char*转换为wchar_t*:
char *str=_T("翔翔糖糖");
int size=MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
wchar_t *ch=new wchar_t[size+1];
if(!MultiByteToWideChar(CP_ACP,0,str,-1,ch,size))
{ return false;}
MultiByteToWideChar使用例子 
char* OleDBCom::UnsignedShortToCharp(unsigned short *strU)

UINT nStrULength=WideCharToMultiByte(
CP_ACP,0,strU,-1,NULL,NULL,NULL,NULL);
LPSTR lpStr;
lpStr=(char*)malloc(nStrULength); 
WideCharToMultiByte(CP_ACP,0,strU,-1,lpStr,nStrULength,NULL,NULL);
return lpStr;
}
unsigned short* OleDBCom::CharpToUnsignedShort(LPSTR str)
{
OLECHAR strU[255];
int nStatus=MultiByteToWideChar(CP_ACP,0,str,-1,strU,255); 
return strU;
}
====================
HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{
   ULONG cCharacters;
   DWORD dwError;
   if (NULL == pszA)
   {
     *ppszW = NULL;
     return NOERROR;
   }
   cCharacters = strlen(pszA)+1;
   *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
   if (NULL == *ppszW)
     return E_OUTOFMEMORY;
   if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,
       *ppszW, cCharacters))
   {
     dwError = GetLastError();
     CoTaskMemFree(*ppszW);
     *ppszW = NULL;
     return HRESULT_FROM_WIN32(dwError);
   }
return NOERROR;
这里有个例子,
PWSTR pWideCharStr;
int nLenOfWideCharStr;
首先计算需要的宽字符串的字符数
nLenOfWideCharStr=MultiByteToWideChar(CP_ACP,0,pMultiByteStr,-1,NULL,0)
这里的pMultiByteStr是要转换的多字节字符。
给pWideCharStr分配内存块
pWideCharStr=HeapAlloc(GetProcessHeap(),0,nLenOfWideCharStr*sizeof(WCHAR));
然后把多字节字符串转换成宽字符串
MultiByteToWideChar(CP_ACP,0,pMultiByteStr,-1,pWideCharStr,nLenOfWideCharStr)

WideCharToMultiByte和MultiByteToWideChar函数的用法
为了支持Unicode编码,需要多字节与宽字节之间的相互转换。
这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,
然后重新阅读《Windows核心编程》,总结出正确的用法。
WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
下面是代码实现:
1. ANSI to Unicode
wstring ANSIToUnicode( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
    0,
    str.c_str(),
    -1,
    NULL,
    0 ); 
wchar_t * pUnicode; 
pUnicode = new    ]; 
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
::MultiByteToWideChar( CP_ACP,
   0,
   str.c_str(),
   -1,
   (LPWSTR)pUnicode,
   unicodeLen ); 
wstring rt; 
rt = ( wchar_t* )pUnicode;
delete pUnicode; 
return rt; 
}
2. Unicode to ANSI
string UnicodeToANSI( const wstring& str )
{
char* pElementText;
int iTextLen;
iTextLen = WideCharToMultiByte( CP_ACP,
   0,
   str.c_str(),
   -1,
   NULL,
    0,
   NULL,
   NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1) );
::WideCharToMultiByte( CP_ACP,
   0,
   str.c_str(),
   -1,
   pElementText,
   iTextLen,
   NULL,
   NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
3. UTF-8 to Unicode
wstring UTF8ToUnicode( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
    0,
    str.c_str(),
    -1,
    NULL,
    0 ); 
wchar_t * pUnicode; 
pUnicode = new wchar_t[unicodeLen+1]; 
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
::MultiByteToWideChar( CP_UTF8,
   0,
   str.c_str(),
    -1,
   (LPWSTR)pUnicode,
   unicodeLen ); 
wstring rt; 
rt = ( wchar_t* )pUnicode;
delete pUnicode; 
return rt; 
}
4. Unicode to UTF-8
string UnicodeToUTF8( const wstring& str )
{
char*   pElementText;
int iTextLen;
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;


#include "stdafx.h" 
#include "string.h" 
#include "stdio.h" 
#include "windows.h" 
int main(int argc, char* argv[]) 

char temp[20]; 
int nLen; 
LPWSTR name = L"CPU"; 
sprintf(temp,"CPU%02d",i); 
nLen = MultiByteToWideChar(CP_ACP,0,temp,-1,NULL,0); 
MultiByteToWideChar(CP_ACP,0,temp,-1,(LPWSTR)name,nLen); 
WideCharToMultiByte(CP_ACP,0,(LPWSTR)name,-1, strs,100,NULL,NULL); 
printf("good: %s \n",name); 
return 0; 

调试时,在MultiByteToWideChar处报错 
MultiByteToWideChar 函数把一个字符串映射为一个宽字符串。被这个函数映射的字符串不必属于多字节字符集。 

    返回值: 
若该函数成功,且 cchMultiByte 为非零值,则返回值是写入由 lpWideCharStr 指向的缓冲区中的宽字符数。

若该函数成功,且 cchMultiByte 为零,则返回值是以宽字符为单位的缓冲区大小值。该缓冲区可以接收转换后的字符串。 
若该函数失败,则返回值为FALSE,调用GetLastErro可获得补充的错误信息。 


1   //--------------------------------------------------------------------------- 
   //函数输入Big5字符,返回Gb简体字符   //两次转换
    //--------------------------------------------------------------------------- 
    AnsiString __fastcall Big2Gb(AnsiString sBig) 
    { 
    char* pszBig5=NULL; //Big5编码的字符 
    wchar_t* wszUnicode=NULL; //Unicode编码的字符 
    char* pszGbt=NULL; //Gb编码的繁体字符 
    char* pszGbs=NULL; //Gb编码的简体字符 
    AnsiString sGb; //返回的字符串 
    int iLen=0; //需要转换的字符数 
    pszBig5=sBig.c_str(); //读入需要转换的字符参数 
    //计算转换的字符数 
    iLen=MultiByteToWideChar (950, 0, pszBig5, -1, NULL,0) ; 
    //给wszUnicode分配内存 
    wszUnicode=new wchar_t[iLen+1]; 
    //转换Big5码到Unicode码,使用了API函数MultiByteToWideChar 
    MultiByteToWideChar (950, 0, pszBig5, -1, wszUnicode,iLen); 
     //计算转换的字符数 
    iLen=WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, NULL,0, NULL, NULL) ; 
    //给pszGbt分配内存 
    pszGbt=new char[iLen+1]; 
    //给pszGbs分配内存 
    pszGbs=new char[iLen+1]; 
    //转换Unicode码到Gb码繁体,使用API函数WideCharToMultiByte 
    WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, pszGbt,iLen, NULL, NULL) ; 
    //转换Gb码繁体到Gb码简体,使用API函数LCMapString 
    LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE, pszGbt, -1, pszGbs, iLen); 
    //返回Gb码简体字符 
    sGb=pszGbs; 
    //释放内存 
    delete [] wszUnicode; 
    delete [] pszGbt; 
    delete [] pszGbs; 
    return sGb; 
    } 
2   //--------------------------------------------------------------------------- 
//函数输入Gb字符,返回Big5字符    //两次转换
    //--------------------------------------------------------------------------- 
    AnsiString __fastcall Gb2Big(AnsiString sGb) 
    { 
    char* pszGbt=NULL; //Gb编码的繁体字符 
    char* pszGbs=NULL; //Gb编码的简体字符 
    wchar_t* wszUnicode=NULL; //Unicode编码的字符 
    char* pszBig5=NULL; //Big5编码的字符 
    AnsiString sBig5; //返回的字符串 
    int iLen=0; //需要转换的字符数 
    pszGbs=sGb.c_str(); //读入需要转换的字符参数 
    //计算转换的字符数 
    iLen=MultiByteToWideChar (936, 0, pszGbs, -1, NULL,0) ; 
    //给pszGbt分配内存 
    pszGbt=new char[iLen*2+1]; 
    //转换Gb码简体到Gb码繁体,使用API函数LCMapString 
LCMapString(0) 
要确保是在unicode环境下,才可使用WideCharToMultiByte

posted on 2010-09-15 10:18  一个人的天空@  阅读(6476)  评论(0编辑  收藏  举报