| 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 |