C/C++ code//**************************************
// 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;
}