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. CString to TCHAR & TCHAR to CString

CString to TCHAR & TCHAR to CString

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 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.
  • P Offline
    P Offline
    Paulraj G
    wrote on last edited by
    #1

    hi... i have to get a string via text(edit)box. then i have to convert it to TCHAR. because my operation should be do in TCHAR. then i have to do some operation. last i to covert once again it to string and display in a edit box. please anybody can help me? regards, Paulraj.G

    paulraj

    C L T 3 Replies Last reply
    0
    • P Paulraj G

      hi... i have to get a string via text(edit)box. then i have to convert it to TCHAR. because my operation should be do in TCHAR. then i have to do some operation. last i to covert once again it to string and display in a edit box. please anybody can help me? regards, Paulraj.G

      paulraj

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You already asked this question yesterday, what was the problem with the answers ? Anyway, a very complete link about CString management (and how to convert them) can be found here[^]


      Cédric Moonen Software developer
      Charting control [v1.2]

      P 1 Reply Last reply
      0
      • C Cedric Moonen

        You already asked this question yesterday, what was the problem with the answers ? Anyway, a very complete link about CString management (and how to convert them) can be found here[^]


        Cédric Moonen Software developer
        Charting control [v1.2]

        P Offline
        P Offline
        Paulraj G
        wrote on last edited by
        #3

        I FOLLEWED YOUR WAY. BUT THE ANSWER WILL BE LIKE "䚚秅嬸해㺌ຮ晗"

        paulraj

        C CPalliniC 2 Replies Last reply
        0
        • P Paulraj G

          I FOLLEWED YOUR WAY. BUT THE ANSWER WILL BE LIKE "䚚秅嬸해㺌ຮ晗"

          paulraj

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          No need to shout. What did you do exactly ? Post some relevant snippet of code.


          Cédric Moonen Software developer
          Charting control [v1.2]

          1 Reply Last reply
          0
          • P Paulraj G

            hi... i have to get a string via text(edit)box. then i have to convert it to TCHAR. because my operation should be do in TCHAR. then i have to do some operation. last i to covert once again it to string and display in a edit box. please anybody can help me? regards, Paulraj.G

            paulraj

            L Offline
            L Offline
            Llasus
            wrote on last edited by
            #5

            Just a quick sample though I think it might be on a brute-force approach. I had problems with TCHAR though, it has some trash characters at the end. TCHAR* tchar; CString message("Hello there"); int temp = message.GetLength()*2; //multiply by 2 because of the CString format tchar =(TCHAR*)malloc(temp+1); //add 1 for the NULL at the end: but doesn't work memset(tchar, NULL, temp+1); memcpy(tchar, message, temp); //copy; now we have the TCHAR //assign the tchar to the CString CString messageback(tchar, temp/2); //divide by 2 since it's the actual number of characters free(tchar); Sorry but still a problem with TCHAR's variable as there are "trash" characters on the end. Maybe others can help. :) Read this article here -- modified at 18:50 Wednesday 7th November, 2007 ouch! sorry bout that. admittedly, I did not really took that much attention to the article. comments noted. lesson learned. X|:-O

            CPalliniC C R T 4 Replies Last reply
            0
            • P Paulraj G

              I FOLLEWED YOUR WAY. BUT THE ANSWER WILL BE LIKE "䚚秅嬸해㺌ຮ晗"

              paulraj

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Probably you made mistake(s) on that way, maybe posting your code snippet will help. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • L Llasus

                Just a quick sample though I think it might be on a brute-force approach. I had problems with TCHAR though, it has some trash characters at the end. TCHAR* tchar; CString message("Hello there"); int temp = message.GetLength()*2; //multiply by 2 because of the CString format tchar =(TCHAR*)malloc(temp+1); //add 1 for the NULL at the end: but doesn't work memset(tchar, NULL, temp+1); memcpy(tchar, message, temp); //copy; now we have the TCHAR //assign the tchar to the CString CString messageback(tchar, temp/2); //divide by 2 since it's the actual number of characters free(tchar); Sorry but still a problem with TCHAR's variable as there are "trash" characters on the end. Maybe others can help. :) Read this article here -- modified at 18:50 Wednesday 7th November, 2007 ouch! sorry bout that. admittedly, I did not really took that much attention to the article. comments noted. lesson learned. X|:-O

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Honestly, I think you've to read (perhaps with more attention ) the article too (and, maybe, related MSDN documentation). Don't blame me for this. :rose:

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • L Llasus

                  Just a quick sample though I think it might be on a brute-force approach. I had problems with TCHAR though, it has some trash characters at the end. TCHAR* tchar; CString message("Hello there"); int temp = message.GetLength()*2; //multiply by 2 because of the CString format tchar =(TCHAR*)malloc(temp+1); //add 1 for the NULL at the end: but doesn't work memset(tchar, NULL, temp+1); memcpy(tchar, message, temp); //copy; now we have the TCHAR //assign the tchar to the CString CString messageback(tchar, temp/2); //divide by 2 since it's the actual number of characters free(tchar); Sorry but still a problem with TCHAR's variable as there are "trash" characters on the end. Maybe others can help. :) Read this article here -- modified at 18:50 Wednesday 7th November, 2007 ouch! sorry bout that. admittedly, I did not really took that much attention to the article. comments noted. lesson learned. X|:-O

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  Llasus wrote:

                  int temp = message.GetLength()*2; //multiply by 2 because of the CString format

                  Why multiply by 2 ? How do you know that a character is 2 bytes long ? It's only the case when UNICODE is enabled.

                  Llasus wrote:

                  tchar =(TCHAR*)malloc(temp+1); //add 1 for the NULL at the end: but doesn't work

                  Don't use malloc in C++. If you had used new, you won't have any problem with calculating the size. You just create an array of TCHAR and the compiler will allocate the correct size depending of UNICODE is set or not.

                  Llasus wrote:

                  memcpy(tchar, message, temp); //copy; now we have the TCHAR

                  Don't use memcpy to copy strings. Prefer to use the macro _tcscpy that will be strcpy if UNICODE is not declared or wscpy if UNICODE is declared (so you don't have to bother about it). If you have garbage values at the end of your string, it's simply because you didn't put a ending zero at the end of your string.


                  Cédric Moonen Software developer
                  Charting control [v1.2]

                  1 Reply Last reply
                  0
                  • L Llasus

                    Just a quick sample though I think it might be on a brute-force approach. I had problems with TCHAR though, it has some trash characters at the end. TCHAR* tchar; CString message("Hello there"); int temp = message.GetLength()*2; //multiply by 2 because of the CString format tchar =(TCHAR*)malloc(temp+1); //add 1 for the NULL at the end: but doesn't work memset(tchar, NULL, temp+1); memcpy(tchar, message, temp); //copy; now we have the TCHAR //assign the tchar to the CString CString messageback(tchar, temp/2); //divide by 2 since it's the actual number of characters free(tchar); Sorry but still a problem with TCHAR's variable as there are "trash" characters on the end. Maybe others can help. :) Read this article here -- modified at 18:50 Wednesday 7th November, 2007 ouch! sorry bout that. admittedly, I did not really took that much attention to the article. comments noted. lesson learned. X|:-O

                    R Offline
                    R Offline
                    Raj Prathap
                    wrote on last edited by
                    #9

                    I'm not getting what you are trying to do with the code. CString is just a wrapper for TCHAR. That is CString has a member variable like below. CString { private: TCHAR *m_buf; //Several other stuffs! }; if your intention is to construct another CString from the edit box contents then CString messageback(message) will suffice. to get the pointer to member variable m_buf(member variable name might not be exact) by using GetBuffer() method. pratap

                    1 Reply Last reply
                    0
                    • P Paulraj G

                      hi... i have to get a string via text(edit)box. then i have to convert it to TCHAR. because my operation should be do in TCHAR. then i have to do some operation. last i to covert once again it to string and display in a edit box. please anybody can help me? regards, Paulraj.G

                      paulraj

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

                      here[^] and be sure "the Notify me by e-mail if someone answers this message" box is checked


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

                      1 Reply Last reply
                      0
                      • L Llasus

                        Just a quick sample though I think it might be on a brute-force approach. I had problems with TCHAR though, it has some trash characters at the end. TCHAR* tchar; CString message("Hello there"); int temp = message.GetLength()*2; //multiply by 2 because of the CString format tchar =(TCHAR*)malloc(temp+1); //add 1 for the NULL at the end: but doesn't work memset(tchar, NULL, temp+1); memcpy(tchar, message, temp); //copy; now we have the TCHAR //assign the tchar to the CString CString messageback(tchar, temp/2); //divide by 2 since it's the actual number of characters free(tchar); Sorry but still a problem with TCHAR's variable as there are "trash" characters on the end. Maybe others can help. :) Read this article here -- modified at 18:50 Wednesday 7th November, 2007 ouch! sorry bout that. admittedly, I did not really took that much attention to the article. comments noted. lesson learned. X|:-O

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

                        you are writing pure crap, and you think it's a correct answer for someone that is lost with his code ? read here[^] for how to consert from one type to the other (and benefit of the whole linked thread to read it, it could be useful for you too)


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

                        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