converting char* to wstring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    converting char* to wstring

    Hi,
    I have to add one string and one integer in an existing string.

    eg. int32 number and wstring Title to an existing wstring.

    how can this be done?
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by bajajv
    Hi,
    I have to add one string and one integer in an existing string.

    eg. int32 number and wstring Title to an existing wstring.

    how can this be done?
    What's a wstring? If you just mean a C++ string, then you can convert the int to a string and use concatonation.. . More info, please.

    Comment

    • bajajv
      New Member
      • Jun 2007
      • 152

      #3
      we are using std::wstring.
      it is different than std::string.. this is what I know about it currently.

      I have
      std::wstring A;
      int32 B;
      std::wstring C;

      I want to add these 3.

      char buff[2];
      sprintf(buff, "%s", B);
      string s = "";
      s.insert(0, buff);
      std::wstring C (s.length(), L" ");
      C.copy(s.begin( ), s.end(), C.begin());
      C.append(A);

      But it is giving error here -> std::wstring C (s.length(), L" ");

      error C2664: cannot convert parameter 1 from 'unsigned int' to 'const std::basic_stri ng<_Elem,_Trait s,_Ax> &'
      1> with
      1> [
      1> _Elem=wchar_t,
      1> _Traits=std::ch ar_traits<wchar _t>,
      1> _Ax=std::alloca tor<wchar_t>
      1> ]

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        Assuming you are referring to a string of wide chars, you can just use the wide analogs to normal C string functions, see wchar.h,
        Specifically, have a look at swscanf().

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          Ah, sorry, I replied at the same time you did.
          If you are using C++, then you can put your string in a stringstream and then append anything you want to it with the usual stream operator <<. For wstring, you use the analog wstringstream which I believe is also defined in <sstream.h>.

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            Originally posted by arnaudk
            Assuming you are referring to a string of wide chars, you can just use the wide analogs to normal C string functions, see wchar.h,
            Specifically, have a look at swscanf().
            Interesting. Are wstrings used to accomodate foreign chars or something? Maybe I'll just google it :-)

            Comment

            • arnaudk
              Contributor
              • Sep 2007
              • 425

              #7
              I've never used them myself to be honest. What I know is that a char is 1 byte or 256 values, enough to describe the ASCII character set but a wchar is bigger and can accommodate extended ascii/unicode. It is used primarily for internationaliz ation as you suggest.

              Comment

              • bajajv
                New Member
                • Jun 2007
                • 152

                #8
                Originally posted by Sick0Fant
                Interesting. Are wstrings used to accomodate foreign chars or something? Maybe I'll just google it :-)
                I guess so, not 100% sure...

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  I've never used them myself to be honest. What I know is that a char is 1 byte or 256 values, enough to describe the ASCII character set but a wchar is bigger and can accommodate extended ascii/unicode. It is used primarily for internationaliz ation as you suggest.
                  Yes. There is something called a C++ byte (independent of what your platform thinks is a byte). This C++ byte is guaranteed to have at least 8 bits. It can have more (like 9, 16, 36, 64, etc.). This minimum guarantee is great when you only need ASCII. But it fails in any kind of larger encoding, like UTF-16. Which is what wstring can handle.

                  Comment

                  Working...