2016-02-11 142 views
0

我需要將來自.NET世界的System :: Char轉換爲C++/CLI世界中的char(本機C++類型)。如何將System :: Char轉換爲C++ char

我相信,傳入的字符是一個正規的ASCII字符,只是在.NET的Unicode世界。所以我從來不需要處理一個沒有直接映射到字符的字符。

回答

1

如何直接轉換它?

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

哦!由於它們確實是兩種不同的尺寸,我甚至都沒有想過要嘗試這種方式。但它工作得很好。多麼尷尬!非常感謝!! – Gordon

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

非常感謝!