Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Convert a String to a char array

Convert a String to a char array

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structures
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Slaru
    wrote on last edited by
    #1

    Hey, I am pretty new to C++ and Visual C++. Managed C++ offers Strings which are very useful. I would like to be able to copy or convert a String to a char array. I need to because I am trying to transfer this String into a function that is unmanaged and excepts char arrays.

    T A 2 Replies Last reply
    0
    • S Slaru

      Hey, I am pretty new to C++ and Visual C++. Managed C++ offers Strings which are very useful. I would like to be able to copy or convert a String to a char array. I need to because I am trying to transfer this String into a function that is unmanaged and excepts char arrays.

      T Offline
      T Offline
      Tom Archer
      wrote on last edited by
      #2

      #include <vcclr.h> ... String* s = S"Hello"; const __wchar_t __pin * str = PtrToStringChars(s); By the way, this is covered in Nish's and my new book on mixing MFC and Managed C++ code. See sig if interested. Cheers, Tom Archer Inside C#,
      Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

      L 1 Reply Last reply
      0
      • T Tom Archer

        #include <vcclr.h> ... String* s = S"Hello"; const __wchar_t __pin * str = PtrToStringChars(s); By the way, this is covered in Nish's and my new book on mixing MFC and Managed C++ code. See sig if interested. Cheers, Tom Archer Inside C#,
        Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        or: __wchar_t __pin * str = &s->ToCharArray()[0]; It's ugly, but a nice hack to convert __gc[] to *. Note: even though the NULL character is not returned by ToCharArray(), the CLR seems to pin it in some "clear" space. :) Any reason why you add the const keyword? Doesnt __pin take care of that? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        T 1 Reply Last reply
        0
        • L leppie

          or: __wchar_t __pin * str = &s->ToCharArray()[0]; It's ugly, but a nice hack to convert __gc[] to *. Note: even though the NULL character is not returned by ToCharArray(), the CLR seems to pin it in some "clear" space. :) Any reason why you add the const keyword? Doesnt __pin take care of that? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          T Offline
          T Offline
          Tom Archer
          wrote on last edited by
          #4

          The PtrToStringChars is more efficient as it simply returns a casted pointer to the string's internal buffer. By comparison, the String::ToCharArray method creates a brand new copy of the string and returns that to the caller. leppie wrote: Any reason why you add the const keyword? The PtrToStringChars function returns a const System::Char* cast of the internal string value. Cheers, Tom Archer Inside C#,
          Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

          L 1 Reply Last reply
          0
          • T Tom Archer

            The PtrToStringChars is more efficient as it simply returns a casted pointer to the string's internal buffer. By comparison, the String::ToCharArray method creates a brand new copy of the string and returns that to the caller. leppie wrote: Any reason why you add the const keyword? The PtrToStringChars function returns a const System::Char* cast of the internal string value. Cheers, Tom Archer Inside C#,
            Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Tom Archer wrote: The PtrToStringChars function returns a const System::Char* cast of the internal string value. But I have never really understood a const parameter, it appears like reverse psychology to me. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

            T 1 Reply Last reply
            0
            • L leppie

              Tom Archer wrote: The PtrToStringChars function returns a const System::Char* cast of the internal string value. But I have never really understood a const parameter, it appears like reverse psychology to me. leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              T Offline
              T Offline
              Tom Archer
              wrote on last edited by
              #6

              const has nothing to do with __pin. The former simply means that you won't change the value. The latter is used so that the CLR will not move the data in memory. Cheers, Tom Archer Inside C#,
              Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

              1 Reply Last reply
              0
              • S Slaru

                Hey, I am pretty new to C++ and Visual C++. Managed C++ offers Strings which are very useful. I would like to be able to copy or convert a String to a char array. I need to because I am trying to transfer this String into a function that is unmanaged and excepts char arrays.

                A Offline
                A Offline
                Anthony_Yio
                wrote on last edited by
                #7

                Simpler way is to use ATLMFC. #include <atlstr.h> // CString support. String* strSomething; CString str = strSomething; But you will need to find a way to pack your ATLMFC7.0 redistributable when you release your app. Hope this helps.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups