Unicode下char型数组转换CString问题

hitwhzhongqiu 2014-09-27 08:28:48
char szTemp[10]="hello";
CString m_string;
m_string.Format(_T("%s"),szTemp);
MessageBox(m_string);

总是会有乱码,请教如何正确显示
...全文
297 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hitwhzhongqiu 2014-09-28
  • 打赏
  • 举报
回复
谢谢大家了 很受用
sumos 2014-09-28
  • 打赏
  • 举报
回复
6楼的不错。 改成智能指针更好。
百事烟 2014-09-28
  • 打赏
  • 举报
回复
// char转WCAHR  
void C2W(const char* szSrc, WCHAR* wszDst, int nMaxLen/*wszDst的字符长度*/)
{
	int vMinLen = MultiByteToWideChar(CP_ACP, 0, szSrc, -1, NULL, 0);
	if(vMinLen > nMaxLen)
	{
		MessageBoxA(NULL, szSrc, "转换成UNICODE字串失败", MB_ICONWARNING);
		return;
	}
	MultiByteToWideChar(CP_ACP, 0, szSrc, -1, wszDst, vMinLen); 
}

// WCHAR转char 
void W2C(const WCHAR* wszSrc, char* szDst, int nMaxLen/*szDst的字符长度*/)
{
	int vMinLen = WideCharToMultiByte(CP_ACP, 0, wszSrc, -1, NULL, 0, NULL, FALSE);
	if(vMinLen > nMaxLen)
	{
		MessageBoxW(NULL, wszSrc, L"转换成ANSI字串失败", MB_ICONWARNING);
		return;
	}
	WideCharToMultiByte(CP_ACP, 0, wszSrc, -1, szDst, vMinLen, NULL, FALSE);
}
百事烟 2014-09-28
  • 打赏
  • 举报
回复
// char转WCAHR  
void C2W(const char* szSrc, WCHAR* wszDst, int nMaxLen)
{
	int vMinLen = MultiByteToWideChar(CP_ACP, 0, szSrc, -1, NULL, 0);
	if(vMinLen > nMaxLen)
	{
		MessageBoxA(NULL, szSrc, "转换成UNICODE字串失败", MB_ICONWARNING);
		return;
	}
	MultiByteToWideChar(CP_ACP, 0, szSrc, -1, wszDst, vMinLen); 
}

// WCHAR转char 
void W2C(const WCHAR* wszSrc, char* szDst, int nMaxLen)
{
	int vMinLen = WideCharToMultiByte(CP_ACP, 0, wszSrc, -1, NULL, 0, NULL, FALSE);
	if(vMinLen > nMaxLen)
	{
		MessageBoxW(NULL, wszSrc, L"转换成ANSI字串失败", MB_ICONWARNING);
		return;
	}
	WideCharToMultiByte(CP_ACP, 0, wszSrc, -1, szDst, vMinLen, NULL, FALSE);
}
tixisong 2014-09-28
  • 打赏
  • 举报
回复


CString ANSIToUnicode(char * szChar)
{
	int len = 0;
	len = MultiByteToWideChar(CP_ACP, 0, szChar, -1, NULL, 0);
	TCHAR * p;
	p = new TCHAR[len + 1];
	memset(p, 0, (len + 1) * sizeof(TCHAR));
	MultiByteToWideChar(CP_ACP, 0, szChar, -1, p, len);

	CString ss;
	ss.Format(_T("%s"), p);
	delete[]p;
	return ss;
}
string UnicodeToANSI(CString & szStr)
{
	int len = 0; 
	len = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0,NULL,NULL);

	char * pElementText;
	pElementText = new char[len + 1];
	memset(pElementText, 0, sizeof(len + 1));
	WideCharToMultiByte(CP_ACP, 0, szStr, -1, pElementText, len, NULL, NULL);

	string ss;
	ss = pElementText;
	delete[]pElementText;
	return ss;

}
xiaohuh421 2014-09-28
  • 打赏
  • 举报
回复
CString已经为你做了一个 =号运算符重载. CString Str; char buf[0x100] = "这是文字"; Str = buf; 4楼的构造函数方式也可以
Eleven 2014-09-27
  • 打赏
  • 举报
回复
直接 char szTemp[10]="hello"; CString m_string(szTemp); 就可以了
信阳毛尖 2014-09-27
  • 打赏
  • 举报
回复
char szTemp[10]="hello"; CString m_string; m_string.Format(_T("%s"),CA2T(szTemp)); MessageBox(m_string); 或者使用MultiByteToWideChar转换吧
yeah2000 2014-09-27
  • 打赏
  • 举报
回复
使用wcstombs
yedou 2014-09-27
  • 打赏
  • 举报
回复
char szTemp[10]="hello"; CStringA m_string(szTemp); MessageBox(CString(m_string));

16,473

社区成员

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

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

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