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. C / C++ / MFC
  4. How to convert CString to char*.

How to convert CString to char*.

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingtutorial
13 Posts 8 Posters 1 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.
  • U uday kiran janaswamy

    Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------

    Uday kiran

    E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #4

    Hi , Try this :

    CString str = "this is a test";
    char* pchar = new char[str.GetLength() + 1];

    memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);

    Eli

    T 1 Reply Last reply
    0
    • K kasturi_haribabu

      try str.GetBuffer(10);

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #5

      GetBuffer a pointer to the internal CString buffer. it is not a good idea to get this modifiable buffer only if a conversion to char* is needed. there are cast operators for that : (LPCTSTR) for instance.


      Don't know where to start ?
      Refer the Forums Guidelines and ask a friend

      [VisualCalc 3.0][Flags Beginner's Guide]

      1 Reply Last reply
      0
      • U uday kiran janaswamy

        Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------

        Uday kiran

        S Offline
        S Offline
        san123pune
        wrote on last edited by
        #6

        CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; //then .... strcpy(pchar, str.GetBuffer()); str.ReleaseBuffer(); It will copy the content from string to char *

        T 1 Reply Last reply
        0
        • E eli15021979

          Hi , Try this :

          CString str = "this is a test";
          char* pchar = new char[str.GetLength() + 1];

          memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);

          Eli

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #7

          eli15021979 wrote:

          CString str = "this is a test";
          char* pchar = new char[str.GetLength() + 1];
          memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);

          don't mix ansi, unicode and "T" strings... i suggest this instead :

          CString str = _T("this is a test");
          TCHAR* pchar = new TCHAR[str.GetLength() + 1];
          memcpy(pchar , (LPCTSTR)str , str.GetLength() + 1);


          Don't know where to start ?
          Refer the Forums Guidelines and ask a friend

          [VisualCalc 3.0][Flags Beginner's Guide]

          1 Reply Last reply
          0
          • S san123pune

            CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; //then .... strcpy(pchar, str.GetBuffer()); str.ReleaseBuffer(); It will copy the content from string to char *

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #8

            nooo, don't use GetBuffer() when only a Cast operator does the job. read my previous answer[^]. use GetBuffer() if you need to modify the CString buffer internally


            Don't know where to start ?
            Refer the Forums Guidelines and ask a friend

            [VisualCalc 3.0][Flags Beginner's Guide]

            1 Reply Last reply
            0
            • U uday kiran janaswamy

              Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------

              Uday kiran

              realJSOPR Online
              realJSOPR Online
              realJSOP
              wrote on last edited by
              #9

              strcpy(pchar, (LPCTSTR)str); strcpy(pchar, (const char*)str); strcpy_s(pchar, str.GetLength()+1, (LPCTSTR)str); strcpy_s(pchar, str.GetLength()+1, (const char*)str);

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              1 Reply Last reply
              0
              • U uday kiran janaswamy

                Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------

                Uday kiran

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #10

                If you want to use GetBuffer then you need to call ReleaseBuffer


                WhiteSky


                M 1 Reply Last reply
                0
                • K kasturi_haribabu

                  try str.GetBuffer(10);

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #11

                  AHHHH *scream* Stop using GetBuffer!!! :laugh: CString is such a nice encapsulation of char data. GetBuffer is rarely needed. There's plenty of methods and operators to manipulate the string. Mark

                  1 Reply Last reply
                  0
                  • H Hamid Taebi

                    If you want to use GetBuffer then you need to call ReleaseBuffer


                    WhiteSky


                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #12

                    WhiteSky wrote:

                    If you want to use GetBuffer then you need to call ReleaseBuffer

                    Only if you change the contents, right?

                    1 Reply Last reply
                    0
                    • U uday kiran janaswamy

                      Hi all, I am troubling to convert from CString to char* in both unicode and debug mode. //Snippet of code. -------------------------------------------------------------------------- CString str = "this is a test"; char* pchar = new char[str.GetLength() + 1]; now how to store the string "this is a test" in pchar character pointer. //-------------------------------------------------------------------------

                      Uday kiran

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #13

                      a CString in unicode builds is a wchar_t array, not a char. If you want to store chars in a CString in a unicode build then use the specific CStringA type. If you want to use the generic CString, you'll need to convert the unicode CString to a char type (using the WideCharToMultiByte() or similar). Mark

                      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