CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Post C++/CLI...convert wchar_t array to System::String^

    Hi. I am currently learning to use Visual C++ from the the Ivor Horton book.

    I was just practicing something and i came across a point where i needed to convert an array of wchar_t elements to a System::String^ type.

    Example:

    String^ parse(String^ str)
    {
    wchar_t ch = ' '
    array<wchar_t>^ returnstr = gcnew array<wchar_t>(10);
    int pos = str->IndexOf(ch);
    str->CopyTo(0, returnstr, 0, pos);

    return returnstr->ToString();
    }

    int main(array<System::String^> ^args)
    {
    String^ str = L"Hello! one two three";
    String^ p = parse(str);

    Console::WriteLine(p);
    return 0;
    }

    --------------------------
    I'm Getting This output>>>

    System.Char[]
    Press any key to continue...

    -------------------------
    My required Output>>>

    Hello!
    Press any key to continue...

    -------------------------

    What my objective for this program is to just display "Hello!" from the "Hello! one two three" string.
    And what i REALLY WANT TO TRY is returning a String^ type from the array of wchar_t elements (returnstr).

    You may modify the code and show it as your answer..

    Thanks!

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: C++/CLI...convert wchar_t array to System::String^

    Dead easy :

    Code:
    const wchar_t *test = L"hello there";
    String ^xx = gcnew String(test);
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: C++/CLI...convert wchar_t array to System::String^

    Or in your case :

    Code:
    wchar_t ch = ' '
    array<wchar_t>^ returnstr = gcnew array<wchar_t>(10);
    int pos = str->IndexOf(ch);
    str->CopyTo(0, returnstr, 0, pos);
    
    return gcnew String(returnstr);
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured