Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Q: Convert std::string to std::wstring using std::ctype widen()

Reply
Thread Tools

Q: Convert std::string to std::wstring using std::ctype widen()

 
 
Jeffrey Walton
Guest
Posts: n/a
 
      11-25-2006
Hi All,

I've done a little homework (I've read responses to similar from P.J.
Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
is what I am performing (Stroustrup's Appendix D recommendation won't
compile in Microsoft VC++ 6.0).

My question is in reference to MultiByte Character Sets. Will this code
perform as expected? I understand every problem has a simple and
elegant solution that is wrong.

I generally use US English or Unicode, so I don't encounter a lot of
issues others may see (a multibyte character using std::string). I have
verified it works with a Hello World sample.

Jeff
Jeffrey Walton

std::string s = "Hello World";
std::ctype<wchar_t> ct;
std::wstring ws;

for( std::string::const_iterator it = s.begin(); it != s.end();
it++ )
{
ws += ct.widen( *it );
}

// http://www.research.att.com/~bs/3rd_loc.pdf
// by Bjourne himself...
// page 28 of the above reference
// or
// The C++ Programming Language, Special Edition
// Section D.4.2.2, p 895 (Full Manual)
//
// const std::locale& loc = s.getloc();
// wchar_t w = std::use_facet< std::ctype<char> >(loc).widen(c);
// does not compile in Microsft's environment...
// getloc() is not a member of std::basic_string< ... > ...

//
// Dietmar Kuehl code
// Does not compile in VC++ 6.0
//
// std::wstring to_wide_string(std::string const& source) {
// typedef std::ctype<wchar_t> CT;
// std::wstring rc;
// rc.resize(source.size());
// CT const& ct = std::use_facet<CT>(std::locale());
// ct.widen(source.data(), source.data() + source.size(),
rc.data());
// return rc;

 
Reply With Quote
 
 
 
 
Jeffrey Walton
Guest
Posts: n/a
 
      11-25-2006
Jeffrey Walton wrote:
> Hi All,
>
> I've done a little homework (I've read responses to similar from P.J.
> Plauger and Dietmar Kuehl), and wanted to verify with the Group.
>
> SNIP...
>
> Jeff
> Jeffrey Walton
>
> std::string s = "Hello World";
> std::ctype<wchar_t> ct;
> std::wstring ws;
>
> for( std::string::const_iterator it = s.begin(); it != s.end();
> it++ )
> {
> ws += ct.widen( *it );
> }
>
> // http://www.research.att.com/~bs/3rd_loc.pdf
> // by Bjourne himself...
> // page 28 of the above reference
> // or
> // The C++ Programming Language, Special Edition
> // Section D.4.2.2, p 895 (Full Manual)
>
> SNIP Code...
>
> SNIP Code...


Before I get flamed for not using std::codecvt, Stroustrup states
(D.4.6 Character Code Conversion, p 925):
The codecvt facet provides conversion between different character sets
when a character is moved between a stream buffer and external
storage...

Jeff
Jeffrey Walton

 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      11-25-2006

Jeffrey Walton wrote:
> Hi All,
>
> I've done a little homework (I've read responses to similar from P.J.
> Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
> is what I am performing (Stroustrup's Appendix D recommendation won't
> compile in Microsoft VC++ 6.0).


<...>

> // Dietmar Kuehl code
> // Does not compile in VC++ 6.0


<..>

Unfortunately the problem is probably VC++6.0 , not the source code.

Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
or VC8.0 or gcc.,

By doing so you will open up a whole new world...

regards
Andy Little

 
Reply With Quote
 
Jeffrey Walton
Guest
Posts: n/a
 
      11-25-2006
kwikius wrote:
> Jeffrey Walton wrote:
> > Hi All,
> >
> > I've done a little homework (I've read responses to similar from P.J.
> > Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
> > is what I am performing (Stroustrup's Appendix D recommendation won't
> > compile in Microsoft VC++ 6.0).

>
> <...>
>
> > // Dietmar Kuehl code
> > // Does not compile in VC++ 6.0

>
> <..>
>
> Unfortunately the problem is probably VC++6.0 , not the source code.
>
> SNIP ...
>
> regards
> Andy Little


Hi Andy,

> Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
> or VC8.0 or gcc.,

Not feasible...

I can't even get the following to compile (from Stroustrup):
wchar_t wc = std::use_facet< std::ctype<wchar_t> >
(std::wcout.getloc()).widen('e') ;

I think I am going to punt if I can't get input on walking the
std::string. Since my example compiles (and uses Ch = widen( c ) ), it
should perform as expected.

Stroustrup again (Section D.4.5, p. 923):
A call widen(c) transforms the character c into its corresponding Ch
value. If Ch's character set provides several characters
corresponding to c, the standard specifies that "the implest reasonable
transformation" be used.

Jeff
Jeffrey Walton

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      11-25-2006
Jeffrey Walton wrote:
> kwikius wrote:
> > Jeffrey Walton wrote:
> > > Hi All,
> > >
> > > I've done a little homework (I've read responses to similar from P.J.
> > > Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
> > > is what I am performing (Stroustrup's Appendix D recommendation won't
> > > compile in Microsoft VC++ 6.0).

> >
> > <...>
> >
> > > // Dietmar Kuehl code
> > > // Does not compile in VC++ 6.0

> >
> > <..>
> >
> > Unfortunately the problem is probably VC++6.0 , not the source code.
> >
> > SNIP ...
> >
> > regards
> > Andy Little

>
> Hi Andy,
>
> > Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
> > or VC8.0 or gcc.,

> Not feasible...


All I can say is that I used to use VC++6 for a long time And at that
time I basically gave up on anything with templates then as nothing
worked. I kind of assumed it was my fault. Anyway I got VC7.1 and
suddenly I started to find I could do things with templates and I
progressed quite rapidly onto metaprogramming and so on and so forth.
IOW VC6 really holds youre work back.

If you are forced to use VC6, I can only suggest getting a private copy
of a more modern compiler for your own use and then trying to convince
the powers that be to let you upgrade to it, by showing them the
difference. Of course then you may end up with C# I guess ...

OTOH you could try some of the boost people. They have a lot of VC6
workarounds, but even they have got fed up with **** VC6 AFAICS and are
spending much less time trying to support it now better technology is
available.



regards
Andy Little

regards
Andy Little

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      11-25-2006

Jeffrey Walton wrote:
> kwikius wrote:
> > Jeffrey Walton wrote:
> > > Hi All,
> > >
> > > I've done a little homework (I've read responses to similar from P.J.
> > > Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below
> > > is what I am performing (Stroustrup's Appendix D recommendation won't
> > > compile in Microsoft VC++ 6.0).

> >
> > <...>
> >
> > > // Dietmar Kuehl code
> > > // Does not compile in VC++ 6.0

> >
> > <..>
> >
> > Unfortunately the problem is probably VC++6.0 , not the source code.
> >
> > SNIP ...
> >
> > regards
> > Andy Little

>
> Hi Andy,
>
> > Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
> > or VC8.0 or gcc.,

> Not feasible...
>
> I can't even get the following to compile (from Stroustrup):
> wchar_t wc = std::use_facet< std::ctype<wchar_t> >
> (std::wcout.getloc()).widen('e') ;
>
> I think I am going to punt if I can't get input on walking the
> std::string. Since my example compiles (and uses Ch = widen( c ) ), it
> should perform as expected.


I seem to remember one 'cute' feature of VC6 was static variables in
function templates Oh they compiled and built fine but they never
seemed to get updated correctly or you would have one and one only
despite several instantiations. These are the sort of 'fun' problems
you get with VC6. Maybe this is what is happening here.

regards
Andy Little

 
Reply With Quote
 
Mathias Gaunard
Guest
Posts: n/a
 
      11-25-2006
Jeffrey Walton wrote:

>> Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
>> or VC8.0 or gcc.,

> Not feasible...


Upgrading MSVC is free.

 
Reply With Quote
 
P.J. Plauger
Guest
Posts: n/a
 
      11-25-2006
"Jeffrey Walton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

>> Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
>> or VC8.0 or gcc.,

> Not feasible...
>
> I can't even get the following to compile (from Stroustrup):
> wchar_t wc = std::use_facet< std::ctype<wchar_t> >
> (std::wcout.getloc()).widen('e') ;


Look at how _USE_FACET is used in the standard headers. Those
macros continue to work right in later versions of VC++ as well.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


 
Reply With Quote
 
Jeffrey Walton
Guest
Posts: n/a
 
      11-25-2006
Mathias Gaunard wrote:
> Jeffrey Walton wrote:
>
> >> Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
> >> or VC8.0 or gcc.,

> > Not feasible...

>
> Upgrading MSVC is free.


Hi Mathias,

Details (I'm currently using VC++ Enterprise Edition)? I'm still busy
at the moment trying to get the code to work with recomendations...

Jeff

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      11-26-2006
Jeffrey Walton wrote:
> Mathias Gaunard wrote:
> > Jeffrey Walton wrote:
> >
> > >> Save yourself the pain of using VC++6 and get hold of a copy ov VC7.1
> > >> or VC8.0 or gcc.,
> > > Not feasible...

> >
> > Upgrading MSVC is free.

>
> Hi Mathias,
>
> Details (I'm currently using VC++ Enterprise Edition)? I'm still busy
> at the moment trying to get the code to work with recomendations...
>
> Jeff


// The destructor of ctype is protected hence you can't practically
construct one yourself except presumably by derivation.
(22.2.1.1)
//#################
std::ctype<wchar_t> ct;
//#################

// In the other example a const reference is used rather than an object
std::ctype<wchar_t> const & ct = std::use_facet<std::ctype<wchar_t>
>(std::locale());


so you will need to do something else AFAICS to gain access.

regards
Andy Little

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Qestion about convert Object to byte[] and convert it back davidxiongcn@gmail.com Java 5 11-04-2006 04:11 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments