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. ATL / WTL / STL
  4. How to convert string to wstring by means of STL and C++ headers only?

How to convert string to wstring by means of STL and C++ headers only?

Scheduled Pinned Locked Moved ATL / WTL / STL
c++tutorialquestion
16 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.
  • A alabax

    like this: wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); return wstring(pw); } -- modified at 14:03 Wednesday 12th April, 2006

    S Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #6

    Look s about right - proof of the pudding's in the testing, of course!

    A 1 Reply Last reply
    0
    • S Stuart Dootson

      Look s about right - proof of the pudding's in the testing, of course!

      A Offline
      A Offline
      alabax
      wrote on last edited by
      #7

      OMG! It's such a shame I did new without delete ;) GC is deep in my mind! wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); wstring ws(pw); delete [] pw; return ws; }

      L S 2 Replies Last reply
      0
      • A alabax

        like this: wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); return wstring(pw); } -- modified at 14:03 Wednesday 12th April, 2006

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #8

        You're not freeing the memory used by pw, unless I've missed something?


        The Rob Blog
        Google Talk: robert.caldecott

        A 1 Reply Last reply
        0
        • A alabax

          OMG! It's such a shame I did new without delete ;) GC is deep in my mind! wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); wstring ws(pw); delete [] pw; return ws; }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #9

          You could always use a vector instead. :)

          vector<wchar_t> pw(l);
          ...
          use_facet<ctype<wchar_t> >(loc).widen(pc,pc+l-1,&pw[0]);
          wstring ws(&pw[0]);


          The Rob Blog
          Google Talk: robert.caldecott

          1 Reply Last reply
          0
          • A alabax

            OMG! It's such a shame I did new without delete ;) GC is deep in my mind! wstring s2w(const string &s) { size_t l=s.size()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; locale loc(""); use_facet>(loc).widen(pc,pc+l-1,pw); wstring ws(pw); delete [] pw; return ws; }

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #10

            As Robert says, you could use std::vector - or you could use boost::scoped_array<wchar_t>[^] instead of wchar_t

            1 Reply Last reply
            0
            • A alabax

              that's the only ;)

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

              yeah, that's what i see, so, why did you say "once again about it" ??? ;P

              A 1 Reply Last reply
              0
              • L Lost User

                You're not freeing the memory used by pw, unless I've missed something?


                The Rob Blog
                Google Talk: robert.caldecott

                A Offline
                A Offline
                alabax
                wrote on last edited by
                #12

                locale global_locale; wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; use_facet>(loc).widen(pc,pc+l-1,pw); pw[l-1]=L'\0'; wstring ws(pw); delete [] pw; return ws; }

                N 1 Reply Last reply
                0
                • A alabax

                  locale global_locale; wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length()+1; const char *pc=s.c_str(); wchar_t *pw=new wchar_t[l]; if (pw==0) throw; use_facet>(loc).widen(pc,pc+l-1,pw); pw[l-1]=L'\0'; wstring ws(pw); delete [] pw; return ws; }

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #13

                  Not exception safe ;) As others suggested, use std::vector or boost::scoped_array instead of new[]-delete[]


                  My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                  A 1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    Not exception safe ;) As others suggested, use std::vector or boost::scoped_array instead of new[]-delete[]


                    My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

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

                    Ok, here comes cute solution :) wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length(); vector pw(l+1); use_facet>(loc).widen(&s[0],&s[l],&pw[0]); wstring ws(&pw[0]); return ws; } widen does not append trailing L'\0'! The question is: does vector always initialize values to 0?

                    J 1 Reply Last reply
                    0
                    • T toxcct

                      yeah, that's what i see, so, why did you say "once again about it" ??? ;P

                      A Offline
                      A Offline
                      alabax
                      wrote on last edited by
                      #15

                      I mean questions like this appear every month on the newsgroups and message boards. =)

                      1 Reply Last reply
                      0
                      • A alabax

                        Ok, here comes cute solution :) wstring s2w(const string &s,const locale &loc=global_locale) { size_t l=s.length(); vector pw(l+1); use_facet>(loc).widen(&s[0],&s[l],&pw[0]); wstring ws(&pw[0]); return ws; } widen does not append trailing L'\0'! The question is: does vector always initialize values to 0?

                        J Offline
                        J Offline
                        Johann Gerell
                        wrote on last edited by
                        #16

                        alabax wrote:

                        The question is: does vector always initialize values to 0?

                        Yes, the default value of a simple type using this vector ctor:

                        explicit vector(size_type count);

                        is 0. For classes, the default ctor is run, if no explicit value is provided in this vector ctor:

                        vector(size_type count, const T& value);

                        . -- The Blog: Bits and Pieces

                        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