如何将Unicode码转换成汉字显示?

cheetah90 2011-09-18 01:43:52
我这里有一个内容是 16进制的unicode码 文本文件(ANSI)编码,例如: 一个txt文件,内容是 \u5f6d\u65af\u9676.....

我使用cstring 读进来的,cstring中的内容就是"\u5f6d\u65af\u9676"。想要把他们转换成汉字。请指点一下~

拜谢拜谢!

PS。这几天在CSDN上看了好多类似的文章,不过对数据类型不是太懂,看得云里雾里,如果有系统的讲解的文章请推荐一下~ 多谢。
...全文
960 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
java123object 2013-05-10
  • 打赏
  • 举报
回复
引用 6 楼 jennyvenus 的回复:
//**************************************
// unicode字符串转ansi字符串
// 返回大于0成功,小于0失败
//**************************************
int
ustr_astr( WCHAR *unicodestr, char *ansistr )
{
	int result = 0;
	try
	{
		int needlen = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, NULL, 0, NULL, NULL );
		if( needlen < 0 )
		{
			return needlen;
		}

		result = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, ansistr, needlen + 1, NULL, NULL );
		if( result < 0 )
		{
			return result;
		}
		return ( result - 1 );
	}
	catch( ... )
	{
		ShowError();
	}
	return result;
}
只能转纯中文的,能不能不管中文或杂带其它字符都可以转的改进方法
chunyou128 2011-09-18
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/en-us/library/2c8d19sb.aspx
cheetah90 2011-09-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 visualeleven 的回复:]
C/C++ code

CString strText(_T("\\u5f6d\\u65af\\u9676"));
int nCount = strText.Replace(_T("\\u"), _T(" 0x"));

wchar_t* buf = new wchar_t[nCount+1];
memset(buf, 0, sizeof(wchar_t)*(n……
[/Quote]

非常感谢!!! 我改了一下~ 终于可以了!!

想请教一下:
1. 为什么CString str("\u5f6d\u65af\u9676")这个样子就直接可以识别为汉字?但是中间如果放变量就不可以?

2. TCHAR* token = _tcstok(strText.GetBuffer(strText.GetLength()), seps);
while(NULL != token)
{
buf[nIndex++] = _tcstoul(token, NULL, 16);
token = _tcstok(NULL, seps);
}
这两行的思路是什么样的呀?我看不太懂~ 谢谢!
Eleven 2011-09-18
  • 打赏
  • 举报
回复

CString strText(_T("\\u5f6d\\u65af\\u9676"));
int nCount = strText.Replace(_T("\\u"), _T(" 0x"));

wchar_t* buf = new wchar_t[nCount+1];
memset(buf, 0, sizeof(wchar_t)*(nCount+1));
int nIndex = 0;

TCHAR seps[] = _T(" ");
TCHAR* token = _tcstok(strText.GetBuffer(strText.GetLength()), seps);
while(NULL != token)
{
buf[nIndex++] = _tcstoul(token, NULL, 16);
token = _tcstok(NULL, seps);
}
strText.ReleaseBuffer();
AfxMessageBox(CString(buf));
delete[] buf;
buf = NULL;
cheetah90 2011-09-18
  • 打赏
  • 举报
回复
还木有解决啊! 大侠请回复我一下吧~T_T
cheetah90 2011-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zwfgdlc 的回复:]
C/C++ code

WCHAR str[4] = {0x5f6d, 0x65af,0x9676};
MessageBox(NULL, str, str, MB_OK);
[/Quote]

不好意思我的0x5f6d, 0x65af,0x9676 这些都是变量,放在CString中
cheetah90 2011-09-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jennyvenus 的回复:]
C/C++ code
//**************************************
// unicode字符串转ansi字符串
// 返回大于0成功,小于0失败
//**************************************
int
ustr_astr( WCHAR *unicodestr, char *ansistr )
{
int ……
[/Quote]

多谢回复,但是

原来存储unicode编码的是CString,CString到Wchar怎么转换啊?
我用了(LPCTSTR)不过没有转换成功,可能是我的工程设置的是多字节字符集的原因吧?
用户 昵称 2011-09-18
  • 打赏
  • 举报
回复
//**************************************
// unicode字符串转ansi字符串
// 返回大于0成功,小于0失败
//**************************************
int
ustr_astr( WCHAR *unicodestr, char *ansistr )
{
int result = 0;
try
{
int needlen = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, NULL, 0, NULL, NULL );
if( needlen < 0 )
{
return needlen;
}

result = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, ansistr, needlen + 1, NULL, NULL );
if( result < 0 )
{
return result;
}
return ( result - 1 );
}
catch( ... )
{
ShowError();
}
return result;
}
zwfgdlc 2011-09-18
  • 打赏
  • 举报
回复

WCHAR str[4] = {0x5f6d, 0x65af,0x9676};
MessageBox(NULL, str, str, MB_OK);
cheetah90 2011-09-18
  • 打赏
  • 举报
回复
不好意思 注明一下: 用watch看CString里面的内容就是"\u5f6d\u65af\u9676"! 我知道CString temp("\u5f6d\u65af\u9676")temp是可以直接转换成汉字。但是试过如果是CString temp(变量)就不行了。
cheetah90 2011-09-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gameslq 的回复:]
用函数WideCharToMultiByte

unsigned short a;
a = 0x9676;
char cBuf[1024]={0};

WideCharToMultiByte(CP_ACP,0,&a,sizeof(short int)/2,cBuf,sizeof(cBuf), NULL,NULL);
AfxMessageBox(cBuf);

5f6d ……
[/Quote]


多谢解答!不过
我是vs2008, 运行时候提示出错:error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'unsigned short *' to 'LPCWSTR'

gameslq 2011-09-18
  • 打赏
  • 举报
回复
用函数WideCharToMultiByte

unsigned short a;
a = 0x9676;
char cBuf[1024]={0};

WideCharToMultiByte(CP_ACP,0,&a,sizeof(short int)/2,cBuf,sizeof(cBuf), NULL,NULL);
AfxMessageBox(cBuf);

5f6d = 彭
65af = 斯
9676 = 陶

cheetah90 2011-09-18
  • 打赏
  • 举报
回复
顶下~ 求助啊!!

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧