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. Conversion from CString to Char * ??

Conversion from CString to Char * ??

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 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.
  • N Offline
    N Offline
    narayanagvs
    wrote on last edited by
    #1

    Hi, I have a string variable m_strAddBlob with value in it.I need to convert it to char * . I do this way char* buf = (char*)(LPCTSTR)m_strAddBlob; //nBytes=m_strAddBlob.GetLength(); For checking whether the string is properly typecasted or not I do the following.... CString str(buf); AfxMessageBox(str); then I get only first character of the string. Any Suggestions would be great help. Thanks

    Today is a gift, that's why it is called the present.

    R V 2 Replies Last reply
    0
    • N narayanagvs

      Hi, I have a string variable m_strAddBlob with value in it.I need to convert it to char * . I do this way char* buf = (char*)(LPCTSTR)m_strAddBlob; //nBytes=m_strAddBlob.GetLength(); For checking whether the string is properly typecasted or not I do the following.... CString str(buf); AfxMessageBox(str); then I get only first character of the string. Any Suggestions would be great help. Thanks

      Today is a gift, that's why it is called the present.

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Use CString::GetBuffer()

      Nobody can give you wiser advice than yourself. - Cicero

      1 Reply Last reply
      0
      • N narayanagvs

        Hi, I have a string variable m_strAddBlob with value in it.I need to convert it to char * . I do this way char* buf = (char*)(LPCTSTR)m_strAddBlob; //nBytes=m_strAddBlob.GetLength(); For checking whether the string is properly typecasted or not I do the following.... CString str(buf); AfxMessageBox(str); then I get only first character of the string. Any Suggestions would be great help. Thanks

        Today is a gift, that's why it is called the present.

        V Offline
        V Offline
        Viorel
        wrote on last edited by
        #3

        A conversion like

        char * buf = (char *)(LPCTSTR)m_strAddBlob;
        

        is not correct in case of Unicode strings -- the result looks like a string consisting of the first character only. I think you first have to convert your string from Unicode to Ansi. In new MFC this can be done like this:

        CStringA ansi(m_strAddBlob);
        

        Then if you actually need a pointer to constant string, then you can use explicit cast:

        const char * buff = ansi;
        

        If you realy need a pointer to non-constant string and modify the string, then use GetBuffer and ReleaseBuffer functions:

        char * buff = ansi.GetBuffer();
        . . .
        ansi.ReleaseBuffer();
        

        I hope this helps.

        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