473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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_tct;
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_tCT;
// 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;

Nov 25 '06 #1
10 10070
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_tct;
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

Nov 25 '06 #2

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

Nov 25 '06 #3
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

Nov 25 '06 #4
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

Nov 25 '06 #5

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

Nov 25 '06 #6
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.

Nov 25 '06 #7
"Jeffrey Walton" <no******@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr 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
Nov 25 '06 #8
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

Nov 25 '06 #9
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_tct;
//#################

// In the other example a const reference is used rather than an object
std::ctype<wchar_tconst & 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

Nov 26 '06 #10
Jeffrey Walton wrote:
Details (I'm currently using VC++ Enterprise Edition)?
Microsoft Visual C++ 2005 Express Edition is free. You can download it
from MSDN. I think the IDE isn't as good as the one from Visual Studio
though.

You can also configure your MSVC6 IDE to actually use the newer compiler
if you want also.

Nov 26 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: jcrouse | last post by:
I decided to start a new thread. I have output in xml file format. It looks like this: <P1JoyUp> <Top>326</Top> <Left>54</Left> <Height>23</Height> <Width>100</Width> <Visible>True</Visible>...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
4
by: Jim Langston | last post by:
Is there any builtin lowercase std::string compare? Right now I'm doing this: if ( _stricmp( AmmoTypeText.c_str(), "GunBullet" ) == 0 ) AmmoType = Item_Ammo_GunBullet; Is there anything the...
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
10
by: v4vijayakumar | last post by:
1. why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str()...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
2
by: pvong | last post by:
Newbie learning VB.Net. I have a simple DataReader and I can grab the info. The data is in numeric format like 123.99 and I want a TextBox to just display it just like that. When I use the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.