16,472
社区成员
发帖
与我相关
我的任务
分享
//将字符串转为UCS2 80编码
void convAnsiToUnicode(CString strChars, CString& strUniChars)
{
int uniLen;
uniLen = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, LPCTSTR(strChars),-1, NULL, 0 );
WCHAR* uniChar = new WCHAR[uniLen];
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,LPCTSTR(strChars),-1,uniChar,uniLen);
unsigned char* ch;
ch = (unsigned char*)uniChar;
CString tmp = _T("");
for(int i=0;i <uniLen-1; i++)
{
CString tmp1;
tmp1.Format("%02X",*(ch+1));
CString tmp2;
tmp2.Format("%02X",*ch);
ch += 2;
tmp += tmp1;
tmp += tmp2;
}
if( uniChar )
{
delete uniChar;
}
strUniChars = tmp;
return;
}
//**************************************
// ansi字符串转unicode字符串
// 返回大于0成功,小于0失败
//**************************************
int
astr_ustr( char *ansistr, WCHAR *unicodestr )
{
int result = 0;
try
{
//size_t len = strlen( ansistr );
int needlen = MultiByteToWideChar( CP_ACP, 0, ansistr, -1, NULL, 0 );
if( needlen < 0 )
{
return needlen;
}
result = MultiByteToWideChar( CP_ACP, 0, ansistr, -1, unicodestr, needlen + 1 );
if( result < 0 )
{
return result;
}
return wcslen( unicodestr );
}
catch( ... )
{
ShowError();
}
}
//**************************************
// unicode字符串转ucs2hex
// 返回大于0成功,小于0失败
//**************************************
int
ustr_u2hex( WCHAR *unicodestr, size_t length, unsigned char *ucs2hex )
{
size_t i;
int j = 0;
for( i = 0; i < length; i++ )
{
ucs2hex[ j ] = ( ( unicodestr[ i ] >> 8 ) & 0xff );
j++;
ucs2hex[ j ] = ( unicodestr[ i ] & 0xff );
j++;
}
ucs2hex[ j ] = 0;
return j;
}
//**************************************
// ansi字符串转ucs2 hex
// 先转成unicode,再转成ucs2
// 返回大于0成功,小于0失败
//**************************************
int
astr_u2hex( char *ansistr, unsigned char *ucs2hex, size_t *length )
{
int l;
size_t ansilength;
ansilength = strlen( ansistr );
WCHAR *unicodestr = new WCHAR[ ansilength + 10 ];
l = astr_ustr( ansistr, unicodestr );
l = ustr_u2hex( unicodestr, l, ucs2hex );
*length = ( size_t )l;
delete []unicodestr;
return l;
}
//**************************************
// ansi字符串转unicode字符串
// 返回大于0成功,小于0失败
//**************************************
int
astr_ustr( char *ansistr, WCHAR *unicodestr )
{
int result = 0;
try
{
//size_t len = strlen( ansistr );
int needlen = MultiByteToWideChar( CP_ACP, 0, ansistr, -1, NULL, 0 );
if( needlen < 0 )
{
return needlen;
}
result = MultiByteToWideChar( CP_ACP, 0, ansistr, -1, unicodestr, needlen + 1 );
if( result < 0 )
{
return result;
}
return wcslen( unicodestr );
}
catch( ... )
{
ShowError();
}
}
//**************************************
// unicode字符串转ucs2hex
// 返回大于0成功,小于0失败
//**************************************
int
ustr_u2hex( WCHAR *unicodestr, size_t length, unsigned char *ucs2hex )
{
size_t i;
int j = 0;
for( i = 0; i < length; i++ )
{
ucs2hex[ j ] = ( ( unicodestr[ i ] >> 8 ) & 0xff );
j++;
ucs2hex[ j ] = ( unicodestr[ i ] & 0xff );
j++;
}
ucs2hex[ j ] = 0;
return j;
}
//**************************************
// ansi字符串转ucs2 hex
// 先转成unicode,再转成ucs2
// 返回大于0成功,小于0失败
//**************************************
int
astr_u2hex( char *ansistr, unsigned char *ucs2hex, size_t *length )
{
int l;
size_t ansilength;
ansilength = strlen( ansistr );
WCHAR *unicodestr = new WCHAR[ ansilength + 10 ];
l = astr_ustr( ansistr, unicodestr );
l = ustr_u2hex( unicodestr, l, ucs2hex );
*length = ( size_t )l;
delete []unicodestr;
return l;
}
WCHAR * Ansi2Unicode(char *psz, WCHAR *pwsz)
{
int iLen = strlen(psz);
MultiByteToWideChar(CP_ACP, 0, psz,-1,pwsz, iLen+1);
return pwsz;
}