utf8,anscii与unicode之间的转化

//返回的指针需要自己删除
wchar_t* ANSIToUnicode( const char* str )
{
int len = 0;
len
= strlen(str);
int unicodeLen = ::MultiByteToWideChar( CP_ACP,0,str,-1,NULL,0 );
wchar_t
* pUnicode;
pUnicode
= new wchar_t[unicodeLen+1];
memset(pUnicode,
0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
0, str, -1, (LPWSTR)pUnicode, unicodeLen );

return pUnicode;
}

char* UncodeToANSI( wchar_t* str )
{
if ( !str )
{
return NULL;
}

char* pElementText;
int iTextLen;

// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );

pElementText
= new char[iTextLen + 1];
memset( pElementText,
0, iTextLen + 1);

::WideCharToMultiByte( CP_ACP,
0, str, -1, pElementText, iTextLen, NULL, NULL );

return pElementText;
}

wchar_t
* UTF8ToUnicode( const char* str )
{
int len = 0;
len
= strlen( str );

int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str, -1, NULL, 0 );

wchar_t
* pUnicode;
pUnicode
= new wchar_t[ unicodeLen + 1 ];
memset( pUnicode,
0, (unicodeLen + 1 ) * 2 );

::MultiByteToWideChar( CP_UTF8,
0,str,-1,(LPWSTR)pUnicode,unicodeLen );

return pUnicode;
}

char* UnicodeToUTF8( const wchar_t* str )
{
char* pElementText;
int iTextLen;

// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );

pElementText
= new char[iTextLen + 1];
memset( (
void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );

::WideCharToMultiByte( CP_UTF8,
0, str, -1,pElementText, iTextLen, NULL, NULL );
return pElementText;
}

 

 

posted @ 2011-01-17 20:53  狼哥2  阅读(560)  评论(0编辑  收藏  举报