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. String to char*

String to char*

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionvisual-studiohelptutorial
9 Posts 6 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
    Shirani
    wrote on last edited by
    #1

    Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??

    R S O C T 5 Replies Last reply
    0
    • S Shirani

      Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??

      R Offline
      R Offline
      Rane
      wrote on last edited by
      #2

      To convert String to char*, use the function c_str(). This function will return a const pointer to a regular C string(which will be identical to the passed in string). std::string Test1 = "Text to Test"; char *Test = Test1.c_str(); :cool: Regards, Rane

      T 1 Reply Last reply
      0
      • S Shirani

        Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??

        S Offline
        S Offline
        santhoshv84
        wrote on last edited by
        #3

        Hi, You cant use c_str() in CString. You have to use this function to convert a CString to a char *

        char\* ConvertToChar(const CString &s)
        {
        	int nSize = s.GetLength();
        	char \*pAnsiString = new char\[nSize+1\];
        	memset(pAnsiString,0,nSize+1);
        	wcstombs(pAnsiString, s, nSize+1);
        	return pAnsiString;
        }
        

        All the best...

        The price of anything is the amount of life you exchange for it. Thanks and Regards. SANTHOSH V

        1 Reply Last reply
        0
        • S Shirani

          Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??

          O Offline
          O Offline
          onlyjaypatel
          wrote on last edited by
          #4

          if you are using CString there is a method called GetBuffer() CString s; char *ch = s.GetBuffer(s.GetLength()) May be it will solve your problem. plz feedback if you solve it.

          C 1 Reply Last reply
          0
          • O onlyjaypatel

            if you are using CString there is a method called GetBuffer() CString s; char *ch = s.GetBuffer(s.GetLength()) May be it will solve your problem. plz feedback if you solve it.

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            onlyjaypatel wrote:

            if you are using CString there is a method called GetBuffer()

            :omg: What ? GetBuffer is the worst thing you can call to do that. CString provides an operator to do the casting, so if it doesn't work, GetBuffer won't work neither. Also, if you do not call ReleaseBuffer later, your CString object can be corrupted. Please, read this[^].

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            1 Reply Last reply
            0
            • S Shirani

              Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Which kind of string are you talking about ? CString (from the MFC) or std::string (from the STL) ? You probably have UNICODE define, that's probably why you can't 'convert'. I highly suggest that you read this excellent article[^], things will be much clearer afterwards.

              Shirani wrote:

              when i add data member as String or String^

              Are you using managed C++ ? If yes, then this is the wrong forum. This forum is for C++ only, not managed.

              Cédric Moonen Software developer
              Charting control [v1.5] OpenGL game tutorial in C++

              T 1 Reply Last reply
              0
              • S Shirani

                Hey dudes! I am using VS 2005 and doing work in Visual C++ windows Applications. I am facing a prob that i have a class in which my data members are char * but when i get data from text box or edit box it is in the form of String. But i have to convert it into char*. How can i do it?? I got how to convert char * to string by COnvert::ToString(char*). And other one problem is that when i add a class, when i add data member as String or String^ it didn't allow me. To do it which header file Should i include??

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

                this is managed c++ related query and it should be asked in it particular section!

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                Never mind - my own stupidity is the source of every "problem" - Mixture

                cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

                1 Reply Last reply
                0
                • R Rane

                  To convert String to char*, use the function c_str(). This function will return a const pointer to a regular C string(which will be identical to the passed in string). std::string Test1 = "Text to Test"; char *Test = Test1.c_str(); :cool: Regards, Rane

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

                  as per my knowledge System::String doesn't have c_str() function , though std::string generally have.. also it return const char*

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                  Never mind - my own stupidity is the source of every "problem" - Mixture

                  cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Which kind of string are you talking about ? CString (from the MFC) or std::string (from the STL) ? You probably have UNICODE define, that's probably why you can't 'convert'. I highly suggest that you read this excellent article[^], things will be much clearer afterwards.

                    Shirani wrote:

                    when i add data member as String or String^

                    Are you using managed C++ ? If yes, then this is the wrong forum. This forum is for C++ only, not managed.

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    T Offline
                    T Offline
                    ThatsAlok
                    wrote on last edited by
                    #9

                    Cedric Moonen wrote:

                    Are you using managed C++ ? If yes, then this is the wrong forum. This forum is for C++ only, not managed.

                    atleast somebody have sharp common sense here [:)]

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                    Never mind - my own stupidity is the source of every "problem" - Mixture

                    cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>

                    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