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. ATL / WTL / STL
  4. converting CString to Char[100]

converting CString to Char[100]

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorial
10 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.
  • K Offline
    K Offline
    KRISHNARAYALU
    wrote on last edited by
    #1

    I want to Convert CString value to Char[100]; Please guide me. Thanks, Krishna

    A S Z 3 Replies Last reply
    0
    • K KRISHNARAYALU

      I want to Convert CString value to Char[100]; Please guide me. Thanks, Krishna

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      Not sure why char[100]... ??? ...why not get the size first then allocate the array dynamically? But, you can use the CString::GetBufferSetLength() (allows you to specify maximum size) method then just copy the contents of the buffer to the char[] using memcpy().

      K 1 Reply Last reply
      0
      • A Albert Holguin

        Not sure why char[100]... ??? ...why not get the size first then allocate the array dynamically? But, you can use the CString::GetBufferSetLength() (allows you to specify maximum size) method then just copy the contents of the buffer to the char[] using memcpy().

        K Offline
        K Offline
        KRISHNARAYALU
        wrote on last edited by
        #3

        For Example i took char[100] As you mentioned i need to take the length of the string. Thankyou Albert.

        L 1 Reply Last reply
        0
        • K KRISHNARAYALU

          I want to Convert CString value to Char[100]; Please guide me. Thanks, Krishna

          S Offline
          S Offline
          stonexin
          wrote on last edited by
          #4

          LPSTR WideChar2MBCS( const CString& strCS )
          {
          const UINT wLen = strCS.GetLength() + 1;
          UINT aLen = WideCharToMultiByte(CP_ACP,0,strCS,wLen,NULL,0,NULL,NULL);
          LPSTR lpa = new char[aLen];
          WideCharToMultiByte(CP_ACP,0,strCS,wLen,lpa,aLen,NULL,NULL);
          return lpa;
          }

          maybe this will help you. myblog is: http://www.cppblog.com/stonexin/archive/2011/12/01/161280.html

          S J 2 Replies Last reply
          0
          • S stonexin

            LPSTR WideChar2MBCS( const CString& strCS )
            {
            const UINT wLen = strCS.GetLength() + 1;
            UINT aLen = WideCharToMultiByte(CP_ACP,0,strCS,wLen,NULL,0,NULL,NULL);
            LPSTR lpa = new char[aLen];
            WideCharToMultiByte(CP_ACP,0,strCS,wLen,lpa,aLen,NULL,NULL);
            return lpa;
            }

            maybe this will help you. myblog is: http://www.cppblog.com/stonexin/archive/2011/12/01/161280.html

            S Offline
            S Offline
            stonexin
            wrote on last edited by
            #5

            btw,this is unicode project.

            1 Reply Last reply
            0
            • K KRISHNARAYALU

              For Example i took char[100] As you mentioned i need to take the length of the string. Thankyou Albert.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Use strcpy() or its wide character equivalent for the copy. For further information see here[^].

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              A 1 Reply Last reply
              0
              • L Lost User

                Use strcpy() or its wide character equivalent for the copy. For further information see here[^].

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                Forgot you can use that on a CString... :thumbsup:

                L 1 Reply Last reply
                0
                • A Albert Holguin

                  Forgot you can use that on a CString... :thumbsup:

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  If I had $1 for everything I've forgotten ... ;)

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  1 Reply Last reply
                  0
                  • S stonexin

                    LPSTR WideChar2MBCS( const CString& strCS )
                    {
                    const UINT wLen = strCS.GetLength() + 1;
                    UINT aLen = WideCharToMultiByte(CP_ACP,0,strCS,wLen,NULL,0,NULL,NULL);
                    LPSTR lpa = new char[aLen];
                    WideCharToMultiByte(CP_ACP,0,strCS,wLen,lpa,aLen,NULL,NULL);
                    return lpa;
                    }

                    maybe this will help you. myblog is: http://www.cppblog.com/stonexin/archive/2011/12/01/161280.html

                    J Offline
                    J Offline
                    jumancollion
                    wrote on last edited by
                    #9

                    thanks its works.

                    chichewa translation services | urdu translation services | bengali translation services

                    1 Reply Last reply
                    0
                    • K KRISHNARAYALU

                      I want to Convert CString value to Char[100]; Please guide me. Thanks, Krishna

                      Z Offline
                      Z Offline
                      zwhit
                      wrote on last edited by
                      #10

                      CString str = _T( "Test string." );
                      char pBuffer[ 100 ];

                      if( sizeof( TCHAR ) == 2 )
                      WideCharToMultiByte( CP_ACP, 0, ( WCHAR* )str.GetBuffer(), -1, pBuffer, 100, NULL, NULL );
                      else
                      {
                      char* pTemp = ( char* )str.GetBuffer();
                      memcpy_s( pBuffer, 100, pTemp, strlen( pTemp ) + 1 );
                      }

                      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