Connecting Tech Pros Worldwide Forums | Help | Site Map

STL: how to convert wstring to string

gerg
Guest
 
Posts: n/a
#1: Nov 29 '05
How would one convert a wstring to a string?

This is what I have so far:

bool copyto(std::string& l,std::wstring& r)
{
bool ret = false;

size_t i = 0;
const size_t n = r.length()+1;
l.resize(n); // make sure we have enough
for(;i<n;++i)
{
l[i] = r[i];
}
l[i] = 0;
l.resize(n-1);

return ret;
}

which I know is pretty gruesome, but does the job.

the reason for: const size_t n = r.length()+1;
is because I want to make sure I have enough room to transfer the
characters.


roberth+news@ifi.uio.no
Guest
 
Posts: n/a
#2: Nov 29 '05

re: STL: how to convert wstring to string


gerg <jacklig@gmail.com> wrote:
| How would one convert a wstring to a string?

I'm not sure whether or not this is portable, but on many systems one
could, given the wstring containing only characters in the range of
char, do an elementwise copy.

| This is what I have so far:
|
| bool copyto(std::string& l,std::wstring& r)
| {
| bool ret = false;
|
| size_t i = 0;
| const size_t n = r.length()+1;

this is one too many.

| l.resize(n); // make sure we have enough

we don't need the extra byte.

| for(;i<n;++i)
| {
| l[i] = r[i];
| }
| l[i] = 0;

strings are not zero terminated.

| l.resize(n-1);

And here we are removing the last character again.

| return ret;
| }
|
| which I know is pretty gruesome, but does the job.

It would be somewhat simpler if you would not pretend you needed the
extra zero. If that is needed, the string class would take care of the
job.

| the reason for: const size_t n = r.length()+1;
| is because I want to make sure I have enough room to transfer the
| characters.

No, it's for making sure you have enough room to transfer an extra
character too. Anyhow, you are reinventing the weel. The constructor
and some member functions of string takes iterators of different types
as parameters. The code below demonstrates some.

wstring ws = L"Hello";
string s(ws.begin(), ws.end());
s.assign(ws.begin(), ws.end());

--
Robert Bauck Hamar
gerg
Guest
 
Posts: n/a
#3: Nov 29 '05

re: STL: how to convert wstring to string


>> wstring ws = L"Hello";[color=blue][color=green]
>> string s(ws.begin(), ws.end());
>> s.assign(ws.begin(), ws.end());[/color][/color]


sweet, thanks

-greg

gerg
Guest
 
Posts: n/a
#4: Nov 29 '05

re: STL: how to convert wstring to string


example doesn't compile. i didn't realize that string wasn't null
terminated! OK thanks.

Mateusz Loskot
Guest
 
Posts: n/a
#5: Nov 30 '05

re: STL: how to convert wstring to string


roberth+news@ifi.uio.no wrote:[color=blue]
>
> wstring ws = L"Hello";
> string s(ws.begin(), ws.end());
> s.assign(ws.begin(), ws.end());[/color]

Generally, It works but it doesn't take codepage/charset into
consideration.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

roberth+news@ifi.uio.no
Guest
 
Posts: n/a
#6: Dec 7 '05

re: STL: how to convert wstring to string


Mateusz Loskot <mateusz@loskot.net> wrote:
| roberth+news@ifi.uio.no wrote:
| >
| > wstring ws = L"Hello";
| > string s(ws.begin(), ws.end());
| > s.assign(ws.begin(), ws.end());
|
| Generally, It works but it doesn't take codepage/charset into
| consideration.

<quote>
I'm not sure whether or not this is portable, but on many systems one
could, given the wstring containing only characters in the range of
char, do an elementwise copy.
</quote>

That's what my disclaimer is all about.

§ 2.2.3 guarantees that char and wchar_t has some characters in common.
(A-Z, a-z, some whitespace characters, some punctuation characters, and
some control characters.) The specific values are
implementation-defined. That means that your compiler must document
whether or not this should work.
--
Robert Bauck Hamar
Michiel.Salters@tomtom.com
Guest
 
Posts: n/a
#7: Dec 7 '05

re: STL: how to convert wstring to string



roberth+news@ifi.uio.no wrote:[color=blue]
> Mateusz Loskot <mateusz@loskot.net> wrote:
> | roberth+news@ifi.uio.no wrote:
> | >
> | > wstring ws = L"Hello";
> | > string s(ws.begin(), ws.end());
> | > s.assign(ws.begin(), ws.end());
> |
> | Generally, It works but it doesn't take codepage/charset into
> | consideration.
>
> <quote>
> I'm not sure whether or not this is portable, but on many systems one
> could, given the wstring containing only characters in the range of
> char, do an elementwise copy.
> </quote>
>
> That's what my disclaimer is all about.
>
> § 2.2.3 guarantees that char and wchar_t has some characters in common.
> (A-Z, a-z, some whitespace characters, some punctuation characters, and
> some control characters.) The specific values are
> implementation-defined. That means that your compiler must document
> whether or not this should work.[/color]

Well, your original disclaimer didn't exacly mention that even if
wchar_t can hold a superset of char it doesn't imply that the same
character
must have the same value. Furthermore, even your second post isn't
complete.
The compiler doesn't have to document whether the other char values
also
match wchar_t values. This is rarely the case. On systems where wchar_t
is Unicode and char is ISO-8859-1, this is true. On systems where
wchar_t
is Unicode and char is NOT ISO-8859-1, it's not true. Since Europe
needs ISO-8859-15 nowadays, and Windows uses CP1252 for char, don't
count on it. Just try the euro sign (U+20AC)

HTH,
Michiel Salters

Closed Thread