如何将System :: Char转换为C ++ char

时间:2016-02-11 22:08:17

标签: c++-cli

我需要将来自.NET世界的System :: Char转换为C ++ / CLI世界中的char(本机C ++类型)。

我确信传入的字符是常规的ASCII字符,只是在.NET Unicode世界中。所以我永远不必处理没有直接映射到char的字符。

2 个答案:

答案 0 :(得分:1)

直接转换它怎么样?

System::Char neta = System::Char('b');
char c = neta;

答案 1 :(得分:0)

我有一些代码工作,但它正式......非常难看。我希望其他人能为这个问题添加一个比这更好的解决方案的答案!

// 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'.

非常感谢!