在 C++ 中转换类型时出错
我有一个程序需要使用 Format();函数将字符串文字和 int 组合成 CString 变量。我尝试了几种不同的方法来执行此操作,它们的代码在这里:
// declare variables used
CString _CString;
int _int;
// try to use format function with string literal
_CString.Format("text",_int);
// try to use format function with C-Style cast
_CString.Format((wchar_t)"text",_int);
// try to use format function with static_cast
_CString.Format(static_cast<wchar_t>("text"),_int);
第一个返回错误 C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : 无法将参数 1 从 ' const char [33]' to 'const wchar_t *'
对于第二个,没有错误,但文本显示为中文字符。
第三个返回错误 C2440: 'static_cast' : 无法从 'const char [33]' 转换为 'wchar_t'
将 CString 转换为 wchar_t *s 有什么想法吗?
谢谢
I have a program in which I need to use the Format(); function to combine a string literal and a int into a CString variable. I have tried several different ways of doing this, the code for them is here:
// declare variables used
CString _CString;
int _int;
// try to use format function with string literal
_CString.Format("text",_int);
// try to use format function with C-Style cast
_CString.Format((wchar_t)"text",_int);
// try to use format function with static_cast
_CString.Format(static_cast<wchar_t>("text"),_int);
The first one returns error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [33]' to 'const wchar_t *'
For the second, there is no error but the text appears in Chinese characters.
The third one returns error C2440: 'static_cast' : cannot convert from 'const char [33]' to 'wchar_t'
Any ideas for converting CStrings to wchar_t *s?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,目前还不完全清楚您想要定位的字符串类型,但这就是我要做的:
尝试将多字节字符串类型转换为 Unicode 字符串可能会编译,但它会带来麻烦,因为它仍然是一个多字节字符串。 - 字节字符串。如果您想要的话,您需要转换整个字符串,而不仅仅是对其进行类型转换。
Well, it's not entirely clear what string type you'd like to target but here's what I'd be doing:
Attempting to type cast a multi-byte string to a Unicode string may compile, but it's asking for trouble because it's still a multi-byte string. You'll need to convert the entire string and not just typecast it if that's what you want.
问题是您正在执行 UNICODE 构建(这很好),因此
我期望第一个参数是宽字符串的函数。您需要使用
L""
语法来形成宽字符字符串文字:当然,您需要一个说明符来实际将
int
变量格式化为CString:如果包含 tchar.h 标头,则可以使用 Microsoft 的宏将字符串文字设为宽字符或常规旧字符(也称为 ANSI),具体取决于您是否正在构建是否采用 UNICODE:
但我想说,除非您计划支持需要 ANSI 支持的遗留内容,否则我可能不会为
tchar.h
内容烦恼。The problem is that you're performing a UNICODE build (which is fine), so the
function i expecting the first parameter to be a wide character string. You need to use the
L""
syntax to form a wide-character string literal:Of course, you'll need a specifier to actually get the
int
variable formatted into the CString:If you include the
tchar.h
header, you can use Microsoft's macros to make the string literal wide-character or regular-old-character (otherwise known as ANSI) depending on whether you're building UNICODE or not:but I'd say that unless you're planning to support legacy stuff that'll require ANSI support, I probably wouldn't bother with the
tchar.h
stuff.尝试 mbstowcs 函数。
http://msdn.microsoft.com/en-我们/library/ms235631(v=vs.80).aspx
Try the mbstowcs function.
http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx