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. CString conversion problem

CString conversion problem

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

    Hi, i m reading value from ini file and reading into CString.values are stored in japanese language in ini file.now when i convert CString into WCHAR it turns into garbage value.any solution?

    M 1 Reply Last reply
    0
    • U User 589870

      Hi, i m reading value from ini file and reading into CString.values are stored in japanese language in ini file.now when i convert CString into WCHAR it turns into garbage value.any solution?

      M Offline
      M Offline
      Mohan Ramachandra
      wrote on last edited by
      #2

      How are you converting?

      U 1 Reply Last reply
      0
      • M Mohan Ramachandra

        How are you converting?

        U Offline
        U Offline
        User 589870
        wrote on last edited by
        #3

        CString name; WCHAR disname[100]={0}; name= m_IniReader.getKeyValue(str,"VIEWNAME");; wcscpy(disname, A2W((LPCSTR)name)); like this?

        M C A R 4 Replies Last reply
        0
        • U User 589870

          CString name; WCHAR disname[100]={0}; name= m_IniReader.getKeyValue(str,"VIEWNAME");; wcscpy(disname, A2W((LPCSTR)name)); like this?

          M Offline
          M Offline
          Mohan Ramachandra
          wrote on last edited by
          #4

          Try this :)

              WCHAR disname\[100\]={0};
          CStringW name;
              name= m\_IniReader.getKeyValue(str,"VIEWNAME");
          wcscpy(disname, (LPCWSTR)name.GetBuffer(100));
          MessageBoxW(NULL,disname,disname,MB\_OK);
          name.ReleaseBuffer(100);
          
          C 1 Reply Last reply
          0
          • M Mohan Ramachandra

            Try this :)

                WCHAR disname\[100\]={0};
            CStringW name;
                name= m\_IniReader.getKeyValue(str,"VIEWNAME");
            wcscpy(disname, (LPCWSTR)name.GetBuffer(100));
            MessageBoxW(NULL,disname,disname,MB\_OK);
            name.ReleaseBuffer(100);
            
            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Mohan Ramachandra wrote:

            wcscpy(disname, (LPCWSTR)name.GetBuffer(100));

            No, calling GetBuffer is really a very bad practice. You shouldn't do that, the CString class already provides cast operators so there's no need to call GetBuffer ! And the the case is probably no needed neither, but that depends how the project was compiled (UNICODE or not).

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            R M 2 Replies Last reply
            0
            • U User 589870

              CString name; WCHAR disname[100]={0}; name= m_IniReader.getKeyValue(str,"VIEWNAME");; wcscpy(disname, A2W((LPCSTR)name)); like this?

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

              I suggest you read this excellent article[^]. You'll understand much more about string manipulation afterward.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • C Cedric Moonen

                Mohan Ramachandra wrote:

                wcscpy(disname, (LPCWSTR)name.GetBuffer(100));

                No, calling GetBuffer is really a very bad practice. You shouldn't do that, the CString class already provides cast operators so there's no need to call GetBuffer ! And the the case is probably no needed neither, but that depends how the project was compiled (UNICODE or not).

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                R Offline
                R Offline
                rajajay82
                wrote on last edited by
                #7

                i heard a lot of time calling GetBuffer a bad practice, why do so?

                Ajayraj

                C C 2 Replies Last reply
                0
                • U User 589870

                  CString name; WCHAR disname[100]={0}; name= m_IniReader.getKeyValue(str,"VIEWNAME");; wcscpy(disname, A2W((LPCSTR)name)); like this?

                  A Offline
                  A Offline
                  Adam Roderick J
                  wrote on last edited by
                  #8

                  Did u check the whether you are getting the japanese text in CString? And i hope its a UNICODE build.

                  Величие не Бога может быть недооценена.

                  U 1 Reply Last reply
                  0
                  • R rajajay82

                    i heard a lot of time calling GetBuffer a bad practice, why do so?

                    Ajayraj

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

                    GetBuffer (as the name suggests) exposes the internal buffer of the CString object. Moreover you must remember to call ReleaseBuffer when you've done with it. Most of the times, you really don't need to call it. :)

                    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.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    1 Reply Last reply
                    0
                    • R rajajay82

                      i heard a lot of time calling GetBuffer a bad practice, why do so?

                      Ajayraj

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

                      Because if you read the documentation of the function, you will see that you get a pointer to the internal buffer of the string and that you HAVE to call ReleaseBuffer afterward (which will be often forgotten). Furthermore, why would you use GetBuffer, while the CString provides a cast operator which returns you the same thing ? Calling GetBuffer can lead to mistakes if you forget to call ReleaseBuffer.

                      Cédric Moonen Software developer
                      Charting control [v3.0] OpenGL game tutorial in C++

                      1 Reply Last reply
                      0
                      • A Adam Roderick J

                        Did u check the whether you are getting the japanese text in CString? And i hope its a UNICODE build.

                        Величие не Бога может быть недооценена.

                        U Offline
                        U Offline
                        User 589870
                        wrote on last edited by
                        #11

                        yes i m getting japanese text in CString.

                        A 1 Reply Last reply
                        0
                        • C Cedric Moonen

                          Mohan Ramachandra wrote:

                          wcscpy(disname, (LPCWSTR)name.GetBuffer(100));

                          No, calling GetBuffer is really a very bad practice. You shouldn't do that, the CString class already provides cast operators so there's no need to call GetBuffer ! And the the case is probably no needed neither, but that depends how the project was compiled (UNICODE or not).

                          Cédric Moonen Software developer
                          Charting control [v3.0] OpenGL game tutorial in C++

                          M Offline
                          M Offline
                          Mohan Ramachandra
                          wrote on last edited by
                          #12

                          Cedric Moonen wrote:

                          No, calling GetBuffer is really a very bad practice. You shouldn't do that, the CString class already provides cast operators so there's no need to call GetBuffer !

                          Thanks, I didn't know that

                          1 Reply Last reply
                          0
                          • U User 589870

                            yes i m getting japanese text in CString.

                            A Offline
                            A Offline
                            Adam Roderick J
                            wrote on last edited by
                            #13

                            Please check whether _UNICODE preprocessor is added in your project setting.

                            Величие не Бога может быть недооценена.

                            1 Reply Last reply
                            0
                            • U User 589870

                              CString name; WCHAR disname[100]={0}; name= m_IniReader.getKeyValue(str,"VIEWNAME");; wcscpy(disname, A2W((LPCSTR)name)); like this?

                              R Offline
                              R Offline
                              Raj Jaiswal
                              wrote on last edited by
                              #14

                              CString name; WCHAR disname[100]={0}; name= m_IniReader.getKeyValue(str,"VIEWNAME");; wcscpy(disname, A2W((LPCWSTR)name)); Use this code. And make sure that your project build support UNICODE.

                              Raj Jaiswal

                              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