String^ nach char*

Gast #386333
Lesenswert?
• ▲
▼
Okay ich habs gefunden.
Werde es dann nacher mal ausprobieren.


// convert_string_to_wchar.cpp
// compile with: /clr
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >

using namespace System;

int main() {
   String ^str = "Hello";

   // Pin memory so GC can't move it while native function is called
   pin_ptr<const wchar_t> wch = PtrToStringChars(str);
   printf_s("%S\n", wch);

   // Conversion to char* :
   // Can just convert wchar_t* to char* using one of the
   // conversion functions such as:
   // WideCharToMultiByte()
   // wcstombs_s()
   // ... etc
   size_t convertedChars = 0;
   size_t  sizeInBytes = ((str->Length + 1) * 2);
   errno_t err = 0;
   char    *ch = (char *)malloc(sizeInBytes);

   err = wcstombs_s(&convertedChars,
                    ch, sizeInBytes,
                    wch, sizeInBytes);
   if (err != 0)
      printf_s("wcstombs_s  failed!\n");

    printf_s("%s\n", ch);
}
Gast #386338
Lesenswert?
• ▲
▼
array<Byte>^ test; // Managed Equivalent zu unsigned char []
test = System::Text::Encoding::ASCII->GetBytes(str);

> ... Perversion ... Mit C++ hat das nur noch den Namen gemein.
um mal den alten Gag rauszukramen
thesource.ofallevil.com
theroot.ofallevil.com (Verisign)

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren