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 a string to char

How to convert a string to char

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
10 Posts 8 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.
  • V Offline
    V Offline
    vc _fragrance
    wrote on last edited by
    #1

    Hi, Can you say How to convert a CString value to a char value. Thanks in advance.

    R K A C 4 Replies Last reply
    0
    • V vc _fragrance

      Hi, Can you say How to convert a CString value to a char value. Thanks in advance.

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

      CString csStr("Test"); char* pbuff = csStr.GetBuffer(csStr.GetLength()) Rinu Raj

      1 Reply Last reply
      0
      • V vc _fragrance

        Hi, Can you say How to convert a CString value to a char value. Thanks in advance.

        K Offline
        K Offline
        Kiran Pinjala
        wrote on last edited by
        #3

        LPSTR GetBuffer(int;

        KIRAN PINJARLA

        1 Reply Last reply
        0
        • V vc _fragrance

          Hi, Can you say How to convert a CString value to a char value. Thanks in advance.

          A Offline
          A Offline
          ashokbngr
          wrote on last edited by
          #4

          Hi, This link may be helpfull to u? Look into this.. http://www.flounder.com/cstring.htm regds, Ashok

          1 Reply Last reply
          0
          • V vc _fragrance

            Hi, Can you say How to convert a CString value to a char value. Thanks in advance.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Two people have correctly told you how to get a char *, although neither told you that you need to call ReleaseBuffer() on the string when you're done with it. But if you want to access a single char from the string, you can use index notation to access one character at a time. CString s("sucka"); char u = s[1]; This is assuming a non Unicode build, of course.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            S C 2 Replies Last reply
            0
            • C Christian Graus

              Two people have correctly told you how to get a char *, although neither told you that you need to call ReleaseBuffer() on the string when you're done with it. But if you want to access a single char from the string, you can use index notation to access one character at a time. CString s("sucka"); char u = s[1]; This is assuming a non Unicode build, of course.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Nor did they mention that if you don't need to modify the string a simple assignment will do the trick. i.e. LPCTSTR pStr = s;

              Steve

              1 Reply Last reply
              0
              • C Christian Graus

                Two people have correctly told you how to get a char *, although neither told you that you need to call ReleaseBuffer() on the string when you're done with it. But if you want to access a single char from the string, you can use index notation to access one character at a time. CString s("sucka"); char u = s[1]; This is assuming a non Unicode build, of course.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                C Offline
                C Offline
                cpp_prgmer
                wrote on last edited by
                #7

                Well just for curiosity sake, I guess one needs to call ReleaseBuffer() only if the buffer supplied is manipulated with, like data is altered. Does one have to always call ReleaseBuffer(), i mean if he is just calling GetBuffer() just to convert it to form easy for display or copy etc??

                S Z 2 Replies Last reply
                0
                • C cpp_prgmer

                  Well just for curiosity sake, I guess one needs to call ReleaseBuffer() only if the buffer supplied is manipulated with, like data is altered. Does one have to always call ReleaseBuffer(), i mean if he is just calling GetBuffer() just to convert it to form easy for display or copy etc??

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  Every call to GetBuffer should have a matching call to ReleaseBuffer. GetBuffer and ReleaseBuffer should only be used on code which alters the string. If you just want to inspect use operator LPCTSTR. Using this operator is automatic, ie: const char *pData = MyCString; As Christian suggested however, both of these methods should only be use when using a CString with code that knows nothing about CStrings - In general use CString's member functions to manipulate it's contents.

                  Steve

                  C 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    Every call to GetBuffer should have a matching call to ReleaseBuffer. GetBuffer and ReleaseBuffer should only be used on code which alters the string. If you just want to inspect use operator LPCTSTR. Using this operator is automatic, ie: const char *pData = MyCString; As Christian suggested however, both of these methods should only be use when using a CString with code that knows nothing about CStrings - In general use CString's member functions to manipulate it's contents.

                    Steve

                    C Offline
                    C Offline
                    cpp_prgmer
                    wrote on last edited by
                    #9

                    hey, thx for the info. will ensure that for further coding.

                    1 Reply Last reply
                    0
                    • C cpp_prgmer

                      Well just for curiosity sake, I guess one needs to call ReleaseBuffer() only if the buffer supplied is manipulated with, like data is altered. Does one have to always call ReleaseBuffer(), i mean if he is just calling GetBuffer() just to convert it to form easy for display or copy etc??

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      cpp_prgmer wrote:

                      Does one have to always call ReleaseBuffer()

                      Yes. A call to GetBuffer requires a call to ReleaseBuffer. Failing to do so results in a memory leak.

                      cpp_prgmer wrote:

                      i mean if he is just calling GetBuffer() just to convert it to form easy for display or copy etc??

                      There is no need to call GetBuffer for thos purposes. CString has an (LPCTSTR) operator to convert to const TCHAR* implicitly, so in non-UNICODE builds, that operator returns a const char*.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      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