次の方法で共有


方法: char * 文字列を System::Byte 配列に変換する

char * 文字列を Byte 配列に変換する最も効率的な方法は、Marshal クラスを使用することです。

使用例

// convert_native_string_to_Byte_array.cpp
// compile with: /clr
#include <string.h>

using namespace System;
using namespace System::Runtime::InteropServices;

int main() {
   char buf[] = "Native String";
   int len = strlen(buf);

   array< Byte >^ byteArray = gcnew array< Byte >(len + 2);

   // convert native pointer to System::IntPtr with C-Style cast
   Marshal::Copy((IntPtr)buf,byteArray, 0, len);

   for ( int i = byteArray->GetLowerBound(0); i <= byteArray->GetUpperBound(0); i++ ) {
      char dc =  *(Byte^)   byteArray->GetValue(i);
      Console::Write((Char)dc);
   }
   
   Console::WriteLine();
}

Native String

参照

関連項目

C++ Interop (暗黙の PInvoke) の使用