如何转换/(投射)我的“字符串";下面的C ++示例中将值转换为字符? [英] How can I convert/(cast) my "string" values to characters in the following C++ example?
本文介绍了如何转换/(投射)我的“字符串";下面的C ++示例中将值转换为字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#using <System.dll>
using namespace System;
int main()
{
//Two arrays: __wchar_t and String
array<__wchar_t^>^ chararray3 = gcnew array<__wchar_t^>(5);
array<String^>^ strarray = gcnew array<String^>(5);
//values into arrays
chararray3[0] = L'\u2550';
chararray3[1] = L'\u2551';
chararray3[2] = L'\u2552';
chararray3[3] = L'\u2553';
chararray3[4] = L'\u2554';
// note: that compiler generates warning C4566 when generating next array "cannot be represented in current code page (1252)"
strarray[0] = "\u2550";
strarray[1] = "\u2551";
strarray[2] = "\u2552";
strarray[3] = "\u2553";
strarray[4] = "\u2554";
int cp;
// __wchar_t array contents to console ...
for(cp=0;cp<5;cp++)
{
Console::WriteLine(chararray3[cp]);
}
Console::WriteLine(" "); // space
// String array contents to console ... expected result with subbstitution character "?" due to code-page-error/other
for(cp=0;cp<5;cp++)
{
Console::WriteLine(strarray[cp]);
}
// Question is: why isn't THIS gonna happen ... ????
for(cp=0;cp<5;cp++)
{
Console::WriteLine((__wchar_t) strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
}
/* wth ... converting on the fly!
Why not! It's a string but it's also a literal unicode value identical to the ones
contained in the array of __wchar_t that expands to console value without complaint in the above!?
*/
return 0;
}
推荐答案
首先,应将c ++更改为cp ++
To begin with you should change c++ into cp++
for(cp=0;cp<5;cp++)
至于演员,怎么样:
As for the casting, how about:
for(cp=0;cp<5;cp++)
{
Console::WriteLine((System::String ^)strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
}
您不能将string
强制转换为char
.仅仅因为您的字符串中只有一个字符并不能使它成为字符常量,所以它仍然是一个字符串,但其中只有一个字符.
You cannot cast astring
to achar
. Just because you have a single character in your string does not make it into a character constant, it remains a string but with only one character in it.
这篇关于如何转换/(投射)我的“字符串";下面的C ++示例中将值转换为字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文