wstring to string and back

C

Christopher

Googled it and saw several ways to do it. None of which seemed to be
standard C++. Except this one, but it is only from string to wstring.
How do I go back again?

Also can someone verify this is OK?

//----------------------------------------------------------------------------
std::wstring StringToWString(const std::string & s)
{
typedef std::ctype<wchar_t> CT;

std::wstring wide(s.length(), L'\0');

CT const & ct = std::_USE(std::locale(), CT);
ct.widen(&s[0], &s[0] + s.size(), &wide[0]);

return wide;
}
 
B

Boris Schaeling

Googled it and saw several ways to do it. None of which seemed to be
standard C++. Except this one, but it is only from string to wstring.
How do I go back again?

See std::mbstowcs() and std::wcstombs() in <cstdlib>.

Boris
 
D

Daniel T.

Googled it and saw several ways to do it. None of which seemed to be
standard C++. Except this one, but it is only from string to wstring.
How do I go back again?

Also can someone verify this is OK?

//-------------------------------------------------------------------------­---
std::wstring StringToWString(const std::string & s)
{
   typedef std::ctype<wchar_t> CT;

   std::wstring wide(s.length(), L'\0');

   CT const & ct = std::_USE(std::locale(), CT);
   ct.widen(&s[0], &s[0] + s.size(), &wide[0]);

   return wide;
}

(a) I don't think _USE is standard.
(b) Going from wide string to string is as complex as converting from
a long to a short (actually more.)
 
V

Vaclav Haisman

Christopher wrote, On 19.2.2009 8:05:
Googled it and saw several ways to do it. None of which seemed to be
standard C++. Except this one, but it is only from string to wstring.
How do I go back again?

Also can someone verify this is OK?
I am not sure but I do not think that widen() does what what you need but I
am not sure.
//----------------------------------------------------------------------------
std::wstring StringToWString(const std::string & s)
{
typedef std::ctype<wchar_t> CT;

std::wstring wide(s.length(), L'\0');

CT const & ct = std::_USE(std::locale(), CT);
ct.widen(&s[0], &s[0] + s.size(), &wide[0]);

return wide;
}

I have been using the following:

void
towstring_internal (std::wstring & outstr, const char * src, size_t size,
std::locale const & loc)
{
typedef std::codecvt<wchar_t, char, mbstate_t> CodeCvt;
const CodeCvt & cdcvt = std::use_facet<CodeCvt>(loc);
mbstate_t state;
clear_mbstate (state);

char const * from_first = src;
size_t const from_size = size;
char const * const from_last = from_first + from_size;
char const * from_next = from_first;

std::vector<wchar_t> dest (from_size);

wchar_t * to_first = &dest.front ();
size_t to_size = dest.size ();
wchar_t * to_last = to_first + to_size;
wchar_t * to_next = to_first;

CodeCvt::result result;
size_t converted = 0;
while (true)
{
result = cdcvt.in (
state, from_first, from_last,
from_next, to_first, to_last,
to_next);
// XXX: Even if only half of the input has been converted the
// in() method returns CodeCvt::eek:k. I think it should return
// CodeCvt::partial.
if ((result == CodeCvt::partial || result == CodeCvt::eek:k)
&& from_next != from_last)
{
to_size = dest.size () * 2;
dest.resize (to_size);
converted = to_next - to_first;
to_first = &dest.front ();
to_last = to_first + to_size;
to_next = to_first + converted;
continue;
}
else if (result == CodeCvt::eek:k && from_next == from_last)
break;
else if (result == CodeCvt::error
&& to_next != to_last && from_next != from_last)
{
clear_mbstate (state);
++from_next;
from_first = from_next;
*to_next = L'?';
++to_next;
to_first = to_next;
}
else
break;
}
converted = to_next - &dest[0];

outstr.assign (dest.begin (), dest.begin () + converted);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top