Tinyxml中CString如何转换成string

junjie_07 2010-05-14 09:03:30
tinyxml这里不多做介绍,百度一下就知道。

开发平台vs2008,UNICODE

		CString	cstr(_T("发卡机"));
string str(cstr.GetBuffer(cstr.GetLength()));
device->SetAttribute("devName",GBKToUTF8(str));

上面代码编译出错
错误输出为:
error C2440: “初始化”: 无法从“const char *”转换为“ATL::CStringT<BaseType,StringTraits>”
1> with
1> [
1> BaseType=wchar_t,
1> StringTraits=StrTraitMFC_DLL<wchar_t>
1> ]
1> class“ATL::CStringT<BaseType,StringTraits>”的构造函数声明为“explicit”
1> with
1> [
1> BaseType=wchar_t,
1> StringTraits=StrTraitMFC_DLL<wchar_t>
1> ]
1>d:\document files\我的项目\卡片机\mcs\hdpms5.9.5\hdpms\hardwaresetupdlg.cpp(248) : error C2664: “std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)”: 不能将参数 1 从“wchar_t *”转换为“std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it”
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> struct“std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it”的构造函数声明为“explicit”
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]

望高手解决这个问题
其中GBKToUTF8是创建xml文件无法显示中文的解决函数。
//UTF8、GBK之间的转换
string GBKToUTF8(string gbk)
{
//建立一块内存块
char *szOut=new char[gbk.size()+2];
//将内存全设为0;
memset(szOut,0,gbk.size()+2);
//拷贝,相识于Strcpy
memcpy(szOut,gbk.c_str(),strlen(gbk.c_str()));
char* strGBK = szOut;
//映射一个字符串到一个宽字符(unicode)的字符串
int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK, -1, (LPWSTR)wszUtf8, len);
len = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, (LPWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL);
string result=szUtf8;
delete[] szUtf8;
delete[] wszUtf8;
delete[] szOut;
return result;
}
...全文
2386 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
nscboy 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 junjie_07 的回复:]
如何将CString转换为string。
mfc最常用的是CString,而非string。而tinyxml很不幸的是用string的。高手,帮忙看看
[/Quote]

MFC是微软版权的东西啊.
你不能让人家在linux环境中也用这个吧.
做库的当然是首选C++标准库中的string了.
Gary@Tokyo 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 mymtom 的回复:]

不用CString会死啊。
[/Quote]
顶一下这个。
ljz888666555 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 mymtom 的回复:]

不用CString会死啊。
[/Quote]
唉,高手就是不一样,楼主想在MFC中解析XML用CMarkUp也不错。
juniorbeginner 2010-05-20
  • 打赏
  • 举报
回复
这个...首先,你用CString干啥咧
然后,你这个是unicode的工程,你这个CString::GetBuffer出来的其实是wchar_t *,所以不行咧
想简单的话,一个是不用unicode工程,
还有一个方法,其实有这么一个东西叫做wstring...
junjie_07 2010-05-17
  • 打赏
  • 举报
回复
报错:无法从“LPCTSTR”转换为“std::basic_string<_Elem,_Traits,_Ax>”[Quote=引用 3 楼 arong1234 的回复:]
string str = (LPCTSTR) text;

如果text是一个CString,上面代码即可
[/Quote]
junjie_07 2010-05-17
  • 打赏
  • 举报
回复
这个是一定要用的,我检查过,的确有用[Quote=引用 2 楼 logiciel 的回复:]
试一下选用预处理定义TIXML_USE_STL.
[/Quote]
kingstarer 2010-05-15
  • 打赏
  • 举报
回复
提示的跟tinyxml无关

应该是string str(cstr.GetBuffer(cstr.GetLength()));
这行代码出问题了

你得上网找找cstring转string
mstlq 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 mymtom 的回复:]
不用CString会死啊。
[/Quote]

这条回复最管用^_^
mymtom 2010-05-15
  • 打赏
  • 举报
回复
不用CString会死啊。
macrojj 2010-05-15
  • 打赏
  • 举报
回复
你去搜搜 有个人写了个函数 CStringtoCharArray
mstlq 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 arong1234 的回复:]
为什么要转换为char?如果是unicode工程呢?
其实operator LPCTSTR足够了,为什么要额外用一个buffer

引用 4 楼 mstlq 的回复:
用一个
char buffer[512];
使用wcstombs函数将cstr的内容转换到buffer里面……
然后让str = buffer
[/Quote]
从楼主的编译器提示信息来看,他的工程的确是unicode工程……
而楼主用的tinyxml库应该只支持以string或者char*作为参数
arong1234 2010-05-15
  • 打赏
  • 举报
回复
为什么要转换为char?如果是unicode工程呢?
其实operator LPCTSTR足够了,为什么要额外用一个buffer[Quote=引用 4 楼 mstlq 的回复:]
用一个
char buffer[512];
使用wcstombs函数将cstr的内容转换到buffer里面……
然后让str = buffer
[/Quote]
mstlq 2010-05-15
  • 打赏
  • 举报
回复
用一个
char buffer[512];
使用wcstombs函数将cstr的内容转换到buffer里面……
然后让str = buffer
arong1234 2010-05-15
  • 打赏
  • 举报
回复
string str = (LPCTSTR) text;

如果text是一个CString,上面代码即可
logiciel 2010-05-15
  • 打赏
  • 举报
回复
试一下选用预处理定义TIXML_USE_STL.
junjie_07 2010-05-14
  • 打赏
  • 举报
回复
如何将CString转换为string。
mfc最常用的是CString,而非string。而tinyxml很不幸的是用string的。高手,帮忙看看

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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