From: Barry S. <ba...@ba...> - 2009-02-12 21:13:24
|
On 10 Feb 2009, at 17:15, William Newbery wrote: > I want to start using uncicode strings. > > Looking at Py::string I see the method as_unicodestring, however > this returns a std::basic_string<Py_UNICODE> and provides no option > of encodeing... > > Another method is the encode method, this lets me provide the > encoding but just returns another Py::String... > > > What exactly do I need to do to go between a python unicode string > and a std::wstring (where sizeof(wchar_t)==2)in UTF-16 encoding? I take it that Py_UNICODE is 4 on your platform. You could try encode('utf-16') to get a Py::String that is in utf-16. Then use as_std_string() to get a std:string, use c_str() to get a pointer to the contents and cast it to wchar_t. Adding a as_std_wstring would be a reasonable thing to add to PyCXX to make this convenient. as_std_wstring could look inside the Py_Object and avoid a number of conversion steps. Barry |