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 MFC?(Very Urgent............)

How to convert CString to char[] in MFC?(Very Urgent............)

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
24 Posts 10 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.
  • E Eytukan

    When he said CString to char[] it looked apparent it's non-unicode. Otherwise he'd have used TCHARs. But as I said it's still a bad way.

    He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

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

    VuNic wrote:

    When he said CString to char[] it looked apparent it's non-unicode.

    No, because when you never heard about UNICODE (like me when I started) and if you are using the latest visual studio versions where UNICODE is enabled by default, then you are in trouble. When I didn't understand anything about all that stuff, I was still using char* everywhere (instead of TCHAR*) and was forcing casts between everything. I can tell you that my code was a big mess X|

    VuNic wrote:

    Otherwise he'd have used TCHARs

    TCHAR is the generic type, it is not specifically UNICODE. You should use TCHAR as often as possible... Anyway, my whole point was: do not use GetBuffer/ReleaseBuffer and explicit casts, unless you exactly know what you are doing :) .

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

    E 1 Reply Last reply
    0
    • E Eytukan

      "Very-Urgent" = "Please ignore my message". That's the meaning in forums. Anyway, a crude way:

      char ch\[128\];
      CString cs = "YourName";
      
      strcpy(ch, (LPSTR)(LPCTSTR)cs);
      

      You can also use GetBuffer, ReleaseBuffer.

      He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #13

      VuNic wrote:

      You can also use GetBuffer, ReleaseBuffer

      These are reserved: can be used only when Mark Salsbery is logged in. :rolleyes:

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      E M G 3 Replies Last reply
      0
      • C Cedric Moonen

        I'm gonna disagree with you here VuNic. The CString has an LPCTSTR operator, which means that casting to a TCHAR* is automatic. If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly. Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.

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

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #14

        Cedric Moonen wrote:

        Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.

        Noone should suggest GetBuffer/ReleaseBuffer (unless Mark is nearby...) just because of LPCTSTR operator return value isn't the same. (Carlo the Nitpick)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • CPalliniC CPallini

          VuNic wrote:

          You can also use GetBuffer, ReleaseBuffer

          These are reserved: can be used only when Mark Salsbery is logged in. :rolleyes:

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #15

          So we can use it. :D My instinct says Mark is logged in and coming.

          He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

          1 Reply Last reply
          0
          • C Cedric Moonen

            VuNic wrote:

            When he said CString to char[] it looked apparent it's non-unicode.

            No, because when you never heard about UNICODE (like me when I started) and if you are using the latest visual studio versions where UNICODE is enabled by default, then you are in trouble. When I didn't understand anything about all that stuff, I was still using char* everywhere (instead of TCHAR*) and was forcing casts between everything. I can tell you that my code was a big mess X|

            VuNic wrote:

            Otherwise he'd have used TCHARs

            TCHAR is the generic type, it is not specifically UNICODE. You should use TCHAR as often as possible... Anyway, my whole point was: do not use GetBuffer/ReleaseBuffer and explicit casts, unless you exactly know what you are doing :) .

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

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #16

            Yep that would be really a mess in particular if you had used char* specific library functions.

            Cedric Moonen wrote:

            Anyway, my whole point was: do not use GetBuffer/ReleaseBuffer and explicit casts, unless you exactly know what you are doing

            Agreed. :). I feel lazy at times & use these crude ways but only when I'm sure nobody else handles the code.

            He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

            1 Reply Last reply
            0
            • E Eytukan

              "Very-Urgent" = "Please ignore my message". That's the meaning in forums. Anyway, a crude way:

              char ch\[128\];
              CString cs = "YourName";
              
              strcpy(ch, (LPSTR)(LPCTSTR)cs);
              

              You can also use GetBuffer, ReleaseBuffer.

              He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

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

              VuNic wrote:

              strcpy(ch, (LPSTR)(LPCTSTR)cs);

              Why the unnecessary casts?

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              E 1 Reply Last reply
              0
              • D David Crow

                VuNic wrote:

                strcpy(ch, (LPSTR)(LPCTSTR)cs);

                Why the unnecessary casts?

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #18

                Valid question. Actually I typed my reply with a char* . char* ch = (LPSTR)(LPCTSTR)cs; But knew that's a bad example. So changed that to char[] & and put the example as copy-the-buffer but had left the LPSTR caste unremoved . LPSTR is not need actually in this case.

                He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

                D 1 Reply Last reply
                0
                • E Eytukan

                  Valid question. Actually I typed my reply with a char* . char* ch = (LPSTR)(LPCTSTR)cs; But knew that's a bad example. So changed that to char[] & and put the example as copy-the-buffer but had left the LPSTR caste unremoved . LPSTR is not need actually in this case.

                  He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

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

                  VuNic wrote:

                  LPSTR is not need actually in this case.

                  Neither cast is needed. Just use:

                  strcpy(ch, cs);

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  E 1 Reply Last reply
                  0
                  • D David Crow

                    VuNic wrote:

                    LPSTR is not need actually in this case.

                    Neither cast is needed. Just use:

                    strcpy(ch, cs);

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    E Offline
                    E Offline
                    Eytukan
                    wrote on last edited by
                    #20

                    :doh: Yes we've been discussing that it returns a const char by default. The reason why I used LPCTSTR is that I couldn't cast it straight to char*. As CString doesn't support non-const version. I'm stupid I didn't think before typing. So we don't need all these caste craps when you are copying the buffer.

                    He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

                    1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      VuNic wrote:

                      You can also use GetBuffer, ReleaseBuffer

                      These are reserved: can be used only when Mark Salsbery is logged in. :rolleyes:

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

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

                      :laugh: That's right!

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        VuNic wrote:

                        You can also use GetBuffer, ReleaseBuffer

                        These are reserved: can be used only when Mark Salsbery is logged in. :rolleyes:

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        G Offline
                        G Offline
                        Gary R Wheeler
                        wrote on last edited by
                        #22

                        CPallini wrote:

                        These are reserved: can be used only when Mark Salsbery is logged in.

                        These are reserved: can be used only when Mark Salsbery or Gary Wheeler is logged in. There; fixed that up for ya.

                        Software Zen: delete this;
                        Fold With Us![^]

                        CPalliniC 1 Reply Last reply
                        0
                        • G Gary R Wheeler

                          CPallini wrote:

                          These are reserved: can be used only when Mark Salsbery is logged in.

                          These are reserved: can be used only when Mark Salsbery or Gary Wheeler is logged in. There; fixed that up for ya.

                          Software Zen: delete this;
                          Fold With Us![^]

                          CPalliniC Online
                          CPalliniC Online
                          CPallini
                          wrote on last edited by
                          #23

                          Thank you for fixing. BTW: who the hell is 'Gary Wheeler'? :-D

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          In testa che avete, signor di Ceprano?

                          G 1 Reply Last reply
                          0
                          • CPalliniC CPallini

                            Thank you for fixing. BTW: who the hell is 'Gary Wheeler'? :-D

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #24

                            Why, he's my mild-mannered alter ego, of course.

                            Software Zen: delete this;
                            Fold With Us![^]

                            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