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

Convert CString to char*

Scheduled Pinned Locked Moved C / C++ / MFC
question
15 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.
  • U Offline
    U Offline
    USAFHokie80
    wrote on last edited by
    #1

    I'm trying to find an easy way to convert a CString to a char* without having to do it character-by-character. I've tried memcpy unsuccessfully. Anyone have any tricks?

    T P 2 Replies Last reply
    0
    • U USAFHokie80

      I'm trying to find an easy way to convert a CString to a char* without having to do it character-by-character. I've tried memcpy unsuccessfully. Anyone have any tricks?

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

      CString provides a (LPCTSTR) cast operator for this.BTW, what are you trying to achieve ? can't you just use CString::operator[]() or CString::GetAt() to retrieve a character in the CString object ?

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      U 1 Reply Last reply
      0
      • T toxcct

        CString provides a (LPCTSTR) cast operator for this.BTW, what are you trying to achieve ? can't you just use CString::operator[]() or CString::GetAt() to retrieve a character in the CString object ?

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        U Offline
        U Offline
        USAFHokie80
        wrote on last edited by
        #3

        I want to get the entire string into a char array in case I can't figure out how to do something else.

        S 1 Reply Last reply
        0
        • U USAFHokie80

          I'm trying to find an easy way to convert a CString to a char* without having to do it character-by-character. I've tried memcpy unsuccessfully. Anyone have any tricks?

          P Offline
          P Offline
          Perspx
          wrote on last edited by
          #4

          This thread may be of use. Regards, --Perspx

          "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

          U T 2 Replies Last reply
          0
          • P Perspx

            This thread may be of use. Regards, --Perspx

            "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

            U Offline
            U Offline
            USAFHokie80
            wrote on last edited by
            #5

            You rock.

            T 1 Reply Last reply
            0
            • U USAFHokie80

              I want to get the entire string into a char array in case I can't figure out how to do something else.

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

              I believe toxcct meant "what do you want to do that for ?"


              T 1 Reply Last reply
              0
              • P Perspx

                This thread may be of use. Regards, --Perspx

                "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

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

                NO, definitely not ! GetBuffer() is not there for casting purpose ! Moreover, the OP seem not to really know what he wants that for, so instead of saying amen to a query-for-code, better understand why such a thing is asked for... I'm more likely to think he's trying to iterate on every characters of the string, so no need for cast for this ; a simple use of the [] operator would do ! BTW, I'm almost certain that Roger Allen (which is also a great codeproject member) would not reply so nowadays... notice that he replied so, but it was in year 2000 ! at last, read this[^] and tell me if you see somewhere in Microsoft recomendations to use GetBuffer().

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                modified on Friday, August 22, 2008 11:16 AM

                P L 2 Replies Last reply
                0
                • U USAFHokie80

                  You rock.

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

                  USAFHokie80 wrote:

                  You rock

                  Probably, but certainly not on this post... please read my answer to him[^]

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  1 Reply Last reply
                  0
                  • S super_ttd

                    I believe toxcct meant "what do you want to do that for ?"


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

                    super_ttd wrote:

                    what do you want to do that for ?

                    exactly. As I am suspecting the OP to have asked this for a darken reason, I'm not sure casting the CString object would be worth it...

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    1 Reply Last reply
                    0
                    • T toxcct

                      NO, definitely not ! GetBuffer() is not there for casting purpose ! Moreover, the OP seem not to really know what he wants that for, so instead of saying amen to a query-for-code, better understand why such a thing is asked for... I'm more likely to think he's trying to iterate on every characters of the string, so no need for cast for this ; a simple use of the [] operator would do ! BTW, I'm almost certain that Roger Allen (which is also a great codeproject member) would not reply so nowadays... notice that he replied so, but it was in year 2000 ! at last, read this[^] and tell me if you see somewhere in Microsoft recomendations to use GetBuffer().

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      modified on Friday, August 22, 2008 11:16 AM

                      P Offline
                      P Offline
                      Perspx
                      wrote on last edited by
                      #10

                      I was merely posting a link to a thread that was of the same subject - I was not involved with any of those thread posts I thought it would be helpful to USAFHokie80 here.. Regards, --Perspx

                      "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

                      U 1 Reply Last reply
                      0
                      • P Perspx

                        I was merely posting a link to a thread that was of the same subject - I was not involved with any of those thread posts I thought it would be helpful to USAFHokie80 here.. Regards, --Perspx

                        "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

                        U Offline
                        U Offline
                        USAFHokie80
                        wrote on last edited by
                        #11

                        Yeah... GetBuffer doesn't work. But this works: char* str = (char*)(LPCTSTR)str2

                        T M 2 Replies Last reply
                        0
                        • T toxcct

                          NO, definitely not ! GetBuffer() is not there for casting purpose ! Moreover, the OP seem not to really know what he wants that for, so instead of saying amen to a query-for-code, better understand why such a thing is asked for... I'm more likely to think he's trying to iterate on every characters of the string, so no need for cast for this ; a simple use of the [] operator would do ! BTW, I'm almost certain that Roger Allen (which is also a great codeproject member) would not reply so nowadays... notice that he replied so, but it was in year 2000 ! at last, read this[^] and tell me if you see somewhere in Microsoft recomendations to use GetBuffer().

                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                          modified on Friday, August 22, 2008 11:16 AM

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #12

                          toxcct wrote:

                          Moreover, the OP seem not to really know what he wants that for, so instead of saying amen to a query-for-code, better understand why such a thing is asked for...

                          better understand stuff! BAH, we don't need no freakin understanding we just writ cods plezzzzz :laugh:

                          led mike

                          T 1 Reply Last reply
                          0
                          • U USAFHokie80

                            Yeah... GetBuffer doesn't work. But this works: char* str = (char*)(LPCTSTR)str2

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

                            USAFHokie80 wrote:

                            Yeah... GetBuffer doesn't work.

                            hum, correction : GetBuffer DOES work, but it's not there for that. ;P

                            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                            1 Reply Last reply
                            0
                            • L led mike

                              toxcct wrote:

                              Moreover, the OP seem not to really know what he wants that for, so instead of saying amen to a query-for-code, better understand why such a thing is asked for...

                              better understand stuff! BAH, we don't need no freakin understanding we just writ cods plezzzzz :laugh:

                              led mike

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

                              lol... was it urgentz ? I didn't read so, so it means I had time to think about design ! :laugh:

                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                              1 Reply Last reply
                              0
                              • U USAFHokie80

                                Yeah... GetBuffer doesn't work. But this works: char* str = (char*)(LPCTSTR)str2

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

                                That's a horrible cast, and a good example why casts are bad. If you needed the cast to get that to compile then you did something wrong. It should be const char* str = str2; If your CString is always based on a "char" character type, then you should be using a CStringA. Mixing generic and fixed character types negates the usefulness of generic character types (i.e. your code will fail on a Unicode build). Mark

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

                                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