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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. TCHAR to char *

TCHAR to char *

Scheduled Pinned Locked Moved C / C++ / MFC
c++announcement
11 Posts 4 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.
  • R Rajesh_Parameswaran

    Hi, I need to convert TCHAR to char*. TCHAR buff[512]={NULL}; char tempBuff[512]={NULL}; CString strTemp; _stprintf(buff, _T("Hello World")); strTemp = buff; buff = strTemp.GetBuffer(strTemp.GetLength()); but buff seems to be null. I'm unable to convert from TCHAR to char*. Using VC++6.0/MFC. UNICODE VERSION. Thanks in Advance. Rajesh

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

    Rajesh_Parameswaran wrote:

    buff = strTemp.GetBuffer(strTemp.GetLength());

    You can't convert a TCHAR* to a TCHAR[512] so that assignment won't work. What are you trying to do?  Do you need to convert the contents of the TCHAR array to an array of char or just cast the pointer to the array to a different type? Mark

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

    1 Reply Last reply
    0
    • R Rajesh_Parameswaran

      Hi, I need to convert TCHAR to char*. TCHAR buff[512]={NULL}; char tempBuff[512]={NULL}; CString strTemp; _stprintf(buff, _T("Hello World")); strTemp = buff; buff = strTemp.GetBuffer(strTemp.GetLength()); but buff seems to be null. I'm unable to convert from TCHAR to char*. Using VC++6.0/MFC. UNICODE VERSION. Thanks in Advance. Rajesh

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

      I didn't notice your tempBuff variable, so I assume you want something like this:

      TCHAR buff[512]={NULL};
         char tempBuff[512]={NULL};

      _stprintf(buff, _T("Hello World"));

      strcpy(tempBuff, CT2CA(buff));

      Mark

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

      1 Reply Last reply
      0
      • R Rajesh_Parameswaran

        Hi, I need to convert TCHAR to char*. TCHAR buff[512]={NULL}; char tempBuff[512]={NULL}; CString strTemp; _stprintf(buff, _T("Hello World")); strTemp = buff; buff = strTemp.GetBuffer(strTemp.GetLength()); but buff seems to be null. I'm unable to convert from TCHAR to char*. Using VC++6.0/MFC. UNICODE VERSION. Thanks in Advance. Rajesh

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #4

        Use an ATL conversion macro:

        CString s = _T("Hello world!");
        const char* p = T2CA(LPCTSTR(s));

        More here[^] and here[^]

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW

        M 1 Reply Last reply
        0
        • M Michael Dunn

          Use an ATL conversion macro:

          CString s = _T("Hello world!");
          const char* p = T2CA(LPCTSTR(s));

          More here[^] and here[^]

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW

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

          Mike, That doesn't work on VS 2005 which is why I coded my example like I did (besides trying to fit the OP's code).  How does one force the macros to work old-style like you've shown? Thanks, Mark

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

          M 1 Reply Last reply
          0
          • M Mark Salsbery

            Mike, That doesn't work on VS 2005 which is why I coded my example like I did (besides trying to fit the OP's code).  How does one force the macros to work old-style like you've shown? Thanks, Mark

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

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #6

            What about it doesn't work in VC8? Does CT2CA work instead?

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW

            M 2 Replies Last reply
            0
            • M Michael Dunn

              What about it doesn't work in VC8? Does CT2CA work instead?

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW

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

              Note this is just for me, in one test application - MFC/Unicode/VS2005.  I ask because I don't know :) T2CA doesn't compile. CT2CA works like this

              char buff[80];
                 strcpy(buff, CT2CA(_T("Hello World")));

              but doesn't work like this

              CString s = _T("Hello world!");
                 const char* p = CT2CA(LPCTSTR(s));
                 // p is invalid here

              Here's a sample from  ATL and MFC String Conversion Macros[^]

              // Example 3
              // Incorrect use of conversion macros.

              void ExampleFunctionW( LPCWSTR pszW )

              {
               // Create a temporary instance of CW2A,

              // save a pointer to it and then delete
               // the temportary instance.

              LPCSTR pszA = CW2A( pszW );
               // The pszA in the following line is an invalid pointer,
               // as the instance of CW2A has gone out of scope.
               ExampleFunctionA( pszA );

              }

              In the section "A Warning Regarding Temporary Class Instances" there's more details. I just don't know how to use the old ones, which it states are still supported.  I'm missing some detail somewhere :) Mark

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

              1 Reply Last reply
              0
              • M Michael Dunn

                What about it doesn't work in VC8? Does CT2CA work instead?

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Hungarian notation FTW

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

                I found it - need a macro.

                USES_CONVERSION;
                CString s = _T("Hello world!");
                const char* p = T2CA(LPCTSTR(s));
                // p is ok to use until the end of the function! Yay!

                That's better :) To recap:

                USES_CONVERSION;
                CString s = _T("Hello world!");
                const char* p1 = CT2CA(LPCTSTR(s));
                // p1 BAD!
                const char* p2 = T2CA(LPCTSTR(s));
                // p2 GOOD!

                Mark

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

                R 1 Reply Last reply
                0
                • M Mark Salsbery

                  I found it - need a macro.

                  USES_CONVERSION;
                  CString s = _T("Hello world!");
                  const char* p = T2CA(LPCTSTR(s));
                  // p is ok to use until the end of the function! Yay!

                  That's better :) To recap:

                  USES_CONVERSION;
                  CString s = _T("Hello world!");
                  const char* p1 = CT2CA(LPCTSTR(s));
                  // p1 BAD!
                  const char* p2 = T2CA(LPCTSTR(s));
                  // p2 GOOD!

                  Mark

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

                  R Offline
                  R Offline
                  Rajesh_Parameswaran
                  wrote on last edited by
                  #9

                  Hi Mark/Micheal, Thanks for the response. I'm still having problem in using the macro USES_CONVERSION. I'm not using ATL in my project. Could you let me know an alternative solution with VC++6.0 / MFC? thanks in advance, Rajesh

                  M 1 Reply Last reply
                  0
                  • R Rajesh_Parameswaran

                    Hi Mark/Micheal, Thanks for the response. I'm still having problem in using the macro USES_CONVERSION. I'm not using ATL in my project. Could you let me know an alternative solution with VC++6.0 / MFC? thanks in advance, Rajesh

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

                    Hi Rajesh, Michael's solution should work for VC++ 6.  It will NOT work on VC++ 8, so you may want to keep that in mind if you ever plan to migrate to a new version of C++. *EDIT* Actually, it WILL work, but you'll need to add the USES_CONVERSION macro in VC++ 8. Sorry for my confusion :) Mark


                    Last modified: 1hr 10mins after originally posted --

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

                    1 Reply Last reply
                    0
                    • R Rajesh_Parameswaran

                      Hi, I need to convert TCHAR to char*. TCHAR buff[512]={NULL}; char tempBuff[512]={NULL}; CString strTemp; _stprintf(buff, _T("Hello World")); strTemp = buff; buff = strTemp.GetBuffer(strTemp.GetLength()); but buff seems to be null. I'm unable to convert from TCHAR to char*. Using VC++6.0/MFC. UNICODE VERSION. Thanks in Advance. Rajesh

                      O Offline
                      O Offline
                      Obukhov
                      wrote on last edited by
                      #11

                      I found the code on [http://www.wincli.com/?p=72]. These two functions is what you need: // returns number of TCHARs in string int wstrlen(_TCHAR * wstr) { int l_idx = 0; while (((char*)wstr)[l_idx]!=0) l_idx+=2; return l_idx; } // Allocate char string and copy TCHAR->char->string // Don't forget to release the allocated memory!!! char * wstrdup(_TCHAR * wSrc) { int l_idx=0; int l_len = wstrlen(wSrc); char * l_nstr = (char*)malloc(l_len); if (l_nstr) { do { l_nstr[l_idx] = (char)wSrc[l_idx]; l_idx++; } while ((char)wSrc[l_idx]!=0); } nstr[l_idx] = 0; return l_nstr; }

                      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