[英]Convert String^ in c# to CString in c++/CLI
我需要幫助解決在MFC
編寫應用程序時遇到的一個問題。
我在我的應用程序中使用CLR
即Common Language Runtime
來集成 c# API。 但現在我堅持將System::String^
轉換為CString
。 我不能那樣做。
我正在使用以下代碼。
String^ csPass = gcnew String(strPassword.GetBuffer());
array<Byte>^ Value = Encoding::UTF8->GetBytes(csPass);
for (int i = 0; i < Value->Length; i++ )
{
csPass += String::Format( "{0:X2}", Value[ i ] );
}
現在我想將csPass
轉換為CString
。 誰可以幫我這個事。 先感謝您。
得到了我的答案。 感謝您的支持@Elliot Tereschuk。
我已經瀏覽了一些參考資料,例如
和
包含頭文件
#include <msclr/marshal_windows.h>
#include <msclr/marshal.h>
using using namespace msclr::interop;
庫using namespace msclr::interop;
最后我的源代碼是。
String^ csPass = gcnew String(strPassword.GetBuffer());
array<Byte>^ Value = Encoding::UTF8->GetBytes(csPass);
for (int i = 0; i < Value->Length; i++ )
{
csPass += String::Format( "{0:X2}", Value[ i ] );
}
marshal_context^ context = gcnew marshal_context();
const char* str = context->marshal_as<const char*>(csPass);
csMyPass.Format(str);
csMypass
是一個CString
類型變量。 謝謝你的支持。
考慮閱讀有關字符串轉換的MSDN 線程。 此外,以下討論可能對您有用:
使用此材料,您可以了解如何操作,甚至可以發布自己的解決方案作為答案
這對我有用,而且要容易得多:
CString csPassAsCtring = CString(csPass)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.