如何将 System::Char 转换为 C++ 字符
How to convert a System::Char to a C++ char
我需要将来自 .NET 世界的 System::Char 转换为 C++/CLI 世界中的 char(本机 C++ 类型)。
我确信传入的字符是常规的 ASCII 字符,就在 .NET Unicode 世界中。所以我永远不必处理没有直接映射到 char 的字符。
我有一些代码可以工作,但正式...非常难看。我希望其他人能为这个问题添加一个比这更好的解决方案的答案!
// I am given neta externally
System::Char neta = System::Char('b');
// Here is the conversion code - is there a better way?
System::String ^str = gcnew System::String(neta,1);
auto trans = Marshal::StringToHGlobalAnsi(str->ToString());
auto cstr = static_cast<const char*>(trans.ToPointer());
char c = *cstr;
Marshal::FreeHGlobal(trans);
// c contains the expected answer 'b'.
非常感谢!
直接转换怎么样?
System::Char neta = System::Char('b');
char c = neta;
我需要将来自 .NET 世界的 System::Char 转换为 C++/CLI 世界中的 char(本机 C++ 类型)。
我确信传入的字符是常规的 ASCII 字符,就在 .NET Unicode 世界中。所以我永远不必处理没有直接映射到 char 的字符。
我有一些代码可以工作,但正式...非常难看。我希望其他人能为这个问题添加一个比这更好的解决方案的答案!
// I am given neta externally
System::Char neta = System::Char('b');
// Here is the conversion code - is there a better way?
System::String ^str = gcnew System::String(neta,1);
auto trans = Marshal::StringToHGlobalAnsi(str->ToString());
auto cstr = static_cast<const char*>(trans.ToPointer());
char c = *cstr;
Marshal::FreeHGlobal(trans);
// c contains the expected answer 'b'.
非常感谢!
直接转换怎么样?
System::Char neta = System::Char('b');
char c = neta;