(.NET)std::string → System::String への変換

解決


DD.  2006-01-21 09:12:03  No: 60417

DD.です。

再度質問申し訳ありません。
.NETでの開発で、std::string から System::String への変換はどうすればよいのでしょうか?

それぞれが異なるものであり、暗黙の変換やキャストでの変換はできないものであることはわかりましたが、ググってみても System::String → std::string や System::String → char* への変換は多数でてくるのですが。。。


Blue  2006-01-21 09:28:39  No: 60418

普通に
int main()
{
    const char* p = "abcぜんかく";
    std::string str = "ABC全角";
    const wchar_t* wp = L"abcぜんかく";
    std::wstring wstr = L"ABC全角";

    System::String^ s1 = gcnew System::String( p );
    System::String^ s2 = gcnew System::String( str.c_str() );
    System::String^ s3 = gcnew System::String( wp );
    System::String^ s4 = gcnew System::String( wstr.c_str() );

    System::Console::WriteLine( s1 );
    System::Console::WriteLine( s2 );
    System::Console::WriteLine( s3 );
    System::Console::WriteLine( s4 );
}
で上手くいっているようだが。。。


Blue  2006-01-21 09:39:22  No: 60419

>    System::String^ s1 = gcnew System::String( p );
>    System::String^ s2 = gcnew System::String( str.c_str() );

String コンストラクタ (SByte*)
http://msdn2.microsoft.com/ja-jp/library/k9s9t975(ja-jp,VS.80).aspx

>    System::String^ s3 = gcnew System::String( wp );
>    System::String^ s4 = gcnew System::String( wstr.c_str() );

String コンストラクタ (Char*)
http://msdn2.microsoft.com/ja-jp/library/6y4za026(ja-jp,VS.80).aspx」が呼ばれていますね

SByte*の方は
> value パラメータは、ASCII エンコードの文字列を表す配列へのポインタを指定する必要があります。
> つまり、文字列は、ANSI コード ページを使ってエンコードされます。
となっているので、普通につかえるわけですな。


Blue  2006-01-21 10:23:37  No: 60420

というか、代わりのクラスがあるのに無理にstd::stringや
MessageBoxを使う必要性があるのでしょうか?

一応
Microsoft Win32 と Microsoft .NET Framework API との対応
http://www.microsoft.com/japan/msdn/net/general/win32map.asp
# .NET Framework 2.0ではないですが。


DD.  2006-01-21 17:52:45  No: 60421

早々な回答ありがとうございます。

System::String^ s1 = gcnew System::String( p );
あら^^;  ほんとですね。

コンパイルエラーで変換できないよ〜と怒られてしまったのにorz

>というか、代わりのクラスがあるのに無理にstd::stringや
>MessageBoxを使う必要性があるのでしょうか?
前々に使っていたクラスを引っ張ってきて使用したかったもので^^;(console app)
と、いいながらも昔の知識を新しいものに無理やり適合させようとさせてただけか。。。やっぱ新しいもの覚えていかんとだめですね。

Blueさんありがとうございました。<(_ _)>


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加