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. TCHAR to string

TCHAR to string

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
14 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.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, How can i convert a TCHAR to a string? for example:

    TCHAR  infoBuf[INFO_BUFFER_SIZE];
    GetComputerName( infoBuf, &bufCharCount );
    string pc;
    ... convert / assign the value of infoBuf to pc ...
    

    Many Thanx

    The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

    M P 2 Replies Last reply
    0
    • P Programm3r

      Hi all, How can i convert a TCHAR to a string? for example:

      TCHAR  infoBuf[INFO_BUFFER_SIZE];
      GetComputerName( infoBuf, &bufCharCount );
      string pc;
      ... convert / assign the value of infoBuf to pc ...
      

      Many Thanx

      The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

      M Offline
      M Offline
      Mila025
      wrote on last edited by
      #2

      Hi, There is no needed any conversion: pc.assign( bufCharCount ); should work fine.

      ----------- Mila

      P 1 Reply Last reply
      0
      • M Mila025

        Hi, There is no needed any conversion: pc.assign( bufCharCount ); should work fine.

        ----------- Mila

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        Sure about that ? Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'DWORD' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 -- modified at 3:49 Wednesday 13th December, 2006

        The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

        M 1 Reply Last reply
        0
        • P Programm3r

          Sure about that ? Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'DWORD' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 -- modified at 3:49 Wednesday 13th December, 2006

          The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

          M Offline
          M Offline
          Mila025
          wrote on last edited by
          #4

          sorry my mistake not bufCharCount but infoBuf pc.assign( infoBuf );

          ----------- Mila

          P 1 Reply Last reply
          0
          • M Mila025

            sorry my mistake not bufCharCount but infoBuf pc.assign( infoBuf );

            ----------- Mila

            P Offline
            P Offline
            Programm3r
            wrote on last edited by
            #5

            Sorry bro but: Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'TCHAR [32767]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); string pc; pc.assign( infoBuf ); // ERROR

            The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

            M 1 Reply Last reply
            0
            • P Programm3r

              Sorry bro but: Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'TCHAR [32767]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); string pc; pc.assign( infoBuf ); // ERROR

              The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

              M Offline
              M Offline
              Mila025
              wrote on last edited by
              #6

              ok, you use VS2005 (I've just noticed it). There is UNICODE by default. TCHAR is defined as a wchar_t and to cooperating with it is std::wsting. If you really need convet it, you can use e.g. wcstombs or W2A

              ----------- Mila

              P 2 Replies Last reply
              0
              • M Mila025

                ok, you use VS2005 (I've just noticed it). There is UNICODE by default. TCHAR is defined as a wchar_t and to cooperating with it is std::wsting. If you really need convet it, you can use e.g. wcstombs or W2A

                ----------- Mila

                P Offline
                P Offline
                Programm3r
                wrote on last edited by
                #7

                Thnx alot Mila for the help and quick replies .... :) ANSWER: wchar_t infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); wstring pc = infoBuf;

                The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                1 Reply Last reply
                0
                • M Mila025

                  ok, you use VS2005 (I've just noticed it). There is UNICODE by default. TCHAR is defined as a wchar_t and to cooperating with it is std::wsting. If you really need convet it, you can use e.g. wcstombs or W2A

                  ----------- Mila

                  P Offline
                  P Offline
                  Programm3r
                  wrote on last edited by
                  #8

                  Sorry but I have another question ....:sigh: How do I now convert wstring to string, Cause see I have this function that accepts a std:string and not a std::wstring ... :confused: ERROR: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::wstring' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 Thanx in advance (again)

                  The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                  P 1 Reply Last reply
                  0
                  • P Programm3r

                    Hi all, How can i convert a TCHAR to a string? for example:

                    TCHAR  infoBuf[INFO_BUFFER_SIZE];
                    GetComputerName( infoBuf, &bufCharCount );
                    string pc;
                    ... convert / assign the value of infoBuf to pc ...
                    

                    Many Thanx

                    The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                    P Offline
                    P Offline
                    prasad_som
                    wrote on last edited by
                    #9

                    One more way to achieve this. Save one function call,too.

                    TCHAR infoBuf[INFO_BUFFER_SIZE];
                    GetComputerName( infoBuf, &bufCharCount );
                    basic_string<TCHAR> pc(infoBuf);

                    Prasad Notifier using ATL | Operator new[],delete[][^]

                    P 1 Reply Last reply
                    0
                    • P Programm3r

                      Sorry but I have another question ....:sigh: How do I now convert wstring to string, Cause see I have this function that accepts a std:string and not a std::wstring ... :confused: ERROR: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::wstring' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 Thanx in advance (again)

                      The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                      P Offline
                      P Offline
                      Programm3r
                      wrote on last edited by
                      #10

                      Not pretty but it works :wtf::omg: wchar_t infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); wstring pc = infoBuf; char b[INFO_BUFFER_SIZE]; string a; wcstombs(b,infoBuf,INFO_BUFFER_SIZE); strcpy((char*)a.c_str(),(char*)b);

                      The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                      P 1 Reply Last reply
                      0
                      • P prasad_som

                        One more way to achieve this. Save one function call,too.

                        TCHAR infoBuf[INFO_BUFFER_SIZE];
                        GetComputerName( infoBuf, &bufCharCount );
                        basic_string<TCHAR> pc(infoBuf);

                        Prasad Notifier using ATL | Operator new[],delete[][^]

                        P Offline
                        P Offline
                        Programm3r
                        wrote on last edited by
                        #11

                        Thnx Prasad, But my fubction accepts a std::string and when I do this: TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); basic_string pc(infoBuf);
                        this would happen: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 32

                        The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                        A 1 Reply Last reply
                        0
                        • P Programm3r

                          Not pretty but it works :wtf::omg: wchar_t infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); wstring pc = infoBuf; char b[INFO_BUFFER_SIZE]; string a; wcstombs(b,infoBuf,INFO_BUFFER_SIZE); strcpy((char*)a.c_str(),(char*)b);

                          The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                          P Offline
                          P Offline
                          prasad_som
                          wrote on last edited by
                          #12

                          I would suggest some modifications to your code,

                          TCHAR  infoBuf\[INFO\_BUFFER\_SIZE\];
                          DWORD  bufCharCount = INFO\_BUFFER\_SIZE;	
                          GetComputerName( infoBuf, &bufCharCount );
                          string a;
                          

                          #ifdef UNICODE
                          size_t i;
                          char temp[INFO_BUFFER_SIZE];
                          wcstombs_s(&i,temp,(size_t)INFO_BUFFER_SIZE,infoBuf,(size_t)INFO_BUFFER_SIZE);
                          a.assign(temp);
                          #else
                          a.assing(infoBuf);
                          #endif

                          Prasad Notifier using ATL | Operator new[],delete[][^]

                          P 1 Reply Last reply
                          0
                          • P prasad_som

                            I would suggest some modifications to your code,

                            TCHAR  infoBuf\[INFO\_BUFFER\_SIZE\];
                            DWORD  bufCharCount = INFO\_BUFFER\_SIZE;	
                            GetComputerName( infoBuf, &bufCharCount );
                            string a;
                            

                            #ifdef UNICODE
                            size_t i;
                            char temp[INFO_BUFFER_SIZE];
                            wcstombs_s(&i,temp,(size_t)INFO_BUFFER_SIZE,infoBuf,(size_t)INFO_BUFFER_SIZE);
                            a.assign(temp);
                            #else
                            a.assing(infoBuf);
                            #endif

                            Prasad Notifier using ATL | Operator new[],delete[][^]

                            P Offline
                            P Offline
                            Programm3r
                            wrote on last edited by
                            #13

                            Thnx for the pointers Prasad ... I appriciate the help :)

                            The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                            1 Reply Last reply
                            0
                            • P Programm3r

                              Thnx Prasad, But my fubction accepts a std::string and when I do this: TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); basic_string pc(infoBuf);
                              this would happen: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 32

                              The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                              A Offline
                              A Offline
                              anand_study
                              wrote on last edited by
                              #14

                              If your language/tool supports WideCharToMultiByte and MultiByteToWideChar functions, you may use them for ASCII<->UNICODE string conversion. Hope this helps. Anand

                              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