关于汉字编码到Unicode(UCS2)80的转换 在VC中的一个程序中需要将一段汉字转为Unicode(UCS2)80格式的编码发送出去能提供一段可以直接调用的源码吗?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 WCHAR * Ansi2Unicode(char *psz, WCHAR *pwsz){ int iLen = strlen(psz); MultiByteToWideChar(CP_ACP, 0, psz,-1,pwsz, iLen+1); return pwsz;} //**************************************// ansi字符串转unicode字符串// 返回大于0成功,小于0失败//**************************************intastr_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失败//**************************************intustr_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失败//**************************************intastr_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失败//**************************************intastr_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失败//**************************************intustr_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失败//**************************************intastr_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;} //将字符串转为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; } 关于RegCreateKey的用法 如何得到启动进程的文件的文件名? sendmessage() 请问,在调用以下函数直接从数据库中取得一个word文档后是否还要用什么函数将其转换后才能读出? 请教用VC开发程序获取其它可执行程序中内容的问题 急》有什么MSND问题 ? 创建DLL的错误 关于CInternetSession类里的OpenURL()问题. 关于程序中播放声音的问题 我想当一个快乐的程序员.... 把对话框大小设为背景图大小的疑难问题. MFC智能设备应用程序上,如何加载Png图片
{
int iLen = strlen(psz);
MultiByteToWideChar(CP_ACP, 0, psz,-1,pwsz, iLen+1);
return pwsz;
}
// 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;
}
//将字符串转为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;
}