1

I just trying to get x11 window title, and store it in std::wstring. I use such command to get the title

auto req_title = xcb_get_property(conn, 0, window, XCB_ATOM_WM_NAME, XCB_GET_PROPERTY_TYPE_ANY, 0, 100);
auto res_title = xcb_get_property_reply(conn, req_title, nullptr);

After that, I can get title stored in char array. How can I convert this array to wstring?

Christophe
  • 68,716
  • 7
  • 72
  • 138

1 Answers1

1

Current solution

You can use std::wstring_convert to convert a string to or from wstring, using a codecvt to specify the conversion to be performed.

Example of use:

string so=u8"Jérôme Ângle"; 
wstring st; 
wstring_convert<std::codecvt_utf8<wchar_t>,wchar_t> converter;
st = converter.from_bytes(so);

If you have a c-string (array of char), the overloads of from_bytes() will do exactly what you want:

char p[]=u8"Jérôme Ângle";
wstring ws = converter.from_bytes(p);

Online demo

Is it sustainable ?

As pointed out in the comments, C++17 has deprecated codecvt and the wstring_convert utility:

These features are hard to use correctly, and there are doubts whether they are even specified correctly. Users should use dedicated text-processing libraries instead.

In addition, a wstring is based on wchar_t which has a very different encoding on linux systems and on windows systems.

So the first question would be to ask why a wstring is needed at all, and why not just keep utf-8 everywhere.

Depending on the reasons, you may consider to use:

  • ICU and its UnicodeString for a full, in-depth, unicode support
  • boost.locale an its to_utf or utf_to_utf, for common unicode related tasks.
  • utf8-cpp for working with utf8 strings the unicode way (attention, seems not maintained).
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Note wstring_convert and wbuffer_convert and codecvt_utf8 are all deprecated in C++17. – n. m. could be an AI Nov 06 '18 at 18:58
  • `from_bytes()` has overloads that accept a null-terminated `char*` string, and two `char*` iterators denoting a range, so you don't need to wrap your `char` data in a temporary `std::string` if it didn't start life that way to begin with. – Remy Lebeau Nov 06 '18 at 19:00
  • 2
    @Christophe IMO just wrap the `std::wstring_convert` code in neat little functions (eg. `utf8_to_ws(...)` & `ws_to_utf8(...)`) Then you don't spread its use throughout the applications. Then when it is changed you only have to change one function, not all your code. – Galik Nov 06 '18 at 19:08
  • @RemyLebeau thanks for pointing this out! I've edited accordingly. – Christophe Nov 06 '18 at 19:11
  • @Galik wise idea ! Especially when I read "*Users should use dedicated text-processing libraries instead.*" in a [working paper of the standard committee](https://isocpp.org/files/papers/p0636r0.html) – Christophe Nov 06 '18 at 19:14
  • @n.m. I suppose you mean [utf-8 everywhere](http://utf8everywhere.org) :-) – Christophe Nov 06 '18 at 19:31
  • utf-8 everywhere is an excellent paper indeed, but you don't have to follow it religiously, just move away from wchar_t, that's all. – n. m. could be an AI Nov 06 '18 at 19:40
  • I'm on c++17 this won't compile. I need wchar_t because i'm using windows libraries. There must be a solution that compiles on c++17 visual studio and this isn't it. – camccar Aug 26 '19 at 00:15