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* in Visual C++ .NET 2005

How to convert CString to char* in Visual C++ .NET 2005

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharphelptutorialquestion
10 Posts 7 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.
  • Y Offline
    Y Offline
    yellowine
    wrote on last edited by
    #1

    I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006

    D J J M 4 Replies Last reply
    0
    • Y yellowine

      I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      yellowine wrote:

      char* hello1=new char[hello.GetLength()+1];

      Change to wchar_t.


      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

      "There is no death, only a change of worlds." - Native American Proverb

      Y 1 Reply Last reply
      0
      • D David Crow

        yellowine wrote:

        char* hello1=new char[hello.GetLength()+1];

        Change to wchar_t.


        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

        "There is no death, only a change of worlds." - Native American Proverb

        Y Offline
        Y Offline
        yellowine
        wrote on last edited by
        #3

        I use the char* hello1 for an OpenGL function which take strictly a char* parameter. and s wchar_t variable will not work for the gl function.

        D 1 Reply Last reply
        0
        • Y yellowine

          I use the char* hello1 for an OpenGL function which take strictly a char* parameter. and s wchar_t variable will not work for the gl function.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          But does the use of wchar_t get rid of the C2664 error? There are ways to convert between Ansi and Unicode.


          "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

          "There is no death, only a change of worlds." - Native American Proverb

          1 Reply Last reply
          0
          • Y yellowine

            I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006

            J Offline
            J Offline
            Jack Puppy
            wrote on last edited by
            #5

            Unicode is turned on by default in 2005, so CString is a Unicode string, and _tcscpy is the wide string copy. 1) Use CStringA instead of CString 2) Use strcpy instead of _tcscpy CStringA hello("CString"); char* hello1=new char[hello.GetLength()+1]; strcpy(hello1, hello); "My dog worries about the economy. Alpo is up to 99 cents a can. That's almost seven dollars in dog money" - Wacky humour found in a business magazine

            1 Reply Last reply
            0
            • Y yellowine

              I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006

              J Offline
              J Offline
              Jijo Raj
              wrote on last edited by
              #6

              Dear yellowine, If you need to convert a CString to ASCII in a unicode environment, there are several converting macros and functions available. Outof them, one of the easy to use is ATL conversion macro. Please see the code block below. #include "ATLBASE.H" . . . CString csString( L"Hello" ); // The UNICODE string. USES_CONVERSION; // Initilizing the conversion macro. char* pszTemp = W2A( csString ); // Convert UNICODE to ASCII. // **NOTE: The W2A allocates string in the stack.** So if you need // ASCII string on heap, you should manually allocate the memory // and strcpy to it. Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

              J 1 Reply Last reply
              0
              • J Jijo Raj

                Dear yellowine, If you need to convert a CString to ASCII in a unicode environment, there are several converting macros and functions available. Outof them, one of the easy to use is ATL conversion macro. Please see the code block below. #include "ATLBASE.H" . . . CString csString( L"Hello" ); // The UNICODE string. USES_CONVERSION; // Initilizing the conversion macro. char* pszTemp = W2A( csString ); // Convert UNICODE to ASCII. // **NOTE: The W2A allocates string in the stack.** So if you need // ASCII string on heap, you should manually allocate the memory // and strcpy to it. Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

                J Offline
                J Offline
                J5121982
                wrote on last edited by
                #7

                Easy way to convert CString to char* CString str="JAYARAJ"; char *ch= str.GetBuffer(str.GetLength()); JAYARAJ

                R J 2 Replies Last reply
                0
                • J J5121982

                  Easy way to convert CString to char* CString str="JAYARAJ"; char *ch= str.GetBuffer(str.GetLength()); JAYARAJ

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  J5121982 wrote:

                  Easy way to convert CString to char*

                  Which won't work for the case in the question. You'll get the same error that yellowine did.

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  1 Reply Last reply
                  0
                  • J J5121982

                    Easy way to convert CString to char* CString str="JAYARAJ"; char *ch= str.GetBuffer(str.GetLength()); JAYARAJ

                    J Offline
                    J Offline
                    Jijo Raj
                    wrote on last edited by
                    #9

                    As per my understanding, the discuession topic is conversion of CString to char* in unicode envrionment( as UNICODE is default in Visual C++ .NET 2005 from previous posts). Whether this code block will work in a UNICODE defined project? i have tested the same in VS 6.0. But its showing compilation errors. or is this work only with Visual C++ .NET 2005 ? please clarify. I have no experiance with Visual C++ .NET 2005. Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

                    1 Reply Last reply
                    0
                    • Y yellowine

                      I am having trouble converting a CString variable to a char* variable below: CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; _tcscpy(hello1, hello); Error 1 error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'wchar_t *' d:\projects\c++.net\test3\test3 Why it is wrong? Thanks in advance. -- modified at 15:54 Thursday 9th March, 2006

                      M Offline
                      M Offline
                      murali_utr
                      wrote on last edited by
                      #10

                      try this, CString hello("CString"); char* hello1=new char[hello.GetLength()+1]; hello1 = hello.GetBuffer(hello.GetLength()); Have A Nice Day! Murali.M

                      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