5

I found this topic Convert between string, u16string & u32string and the solution (which looks really great) works only using libc++ not libstdc++. Currently libc++ is not usable - it is hard to compile and dont work good on Windows.

Is there any method to convert between these representations using C++11 and libstdc++, which works on all platforms?

I'm especially interested in converting u32_string to string (utf8) and vice versa.

Community
  • 1
  • 1
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
  • 2
    Converting from a 32-bit Unicode string to utf8 is very simple, it's about 10 lines of code. Any reason you can't just write the code? utf8 to a 32-bit Unicode string is not hugely more difficult. These formats are simple and well documented. – john Apr 26 '13 at 10:29
  • 1
    I would love to use proven library and not mess with it myself. I didnt know it is so simple, but I'm sure somebody did it before, so I would love to use it - I really strongly belive in code reusability ;) – Wojciech Danilo Apr 26 '13 at 10:35
  • 3
    There's always [boost.locale](http://www.boost.org/doc/libs/1_53_0/libs/locale/doc/html/index.html) – Cubbi Apr 26 '13 at 16:25
  • To make it easier to debug Unicode text issues (that inevitably any programmer working with text will encounter), it is most likely a good idea to learn the UTF-8 and UTF-16 encoding/decoding algorithms. Once you have learned how Unicode works then you can reassess whether they are worth reusing or writing your own. The C++ standard Unicode conversion functions are in `locale/codecvt`. – Andrew Tomazos Apr 28 '13 at 14:48

2 Answers2

2

There is a way portable way in C++11 to do this through the wstring_convert class.

However, it seems that it is not yet implemented libstdc++ (as of gcc 4.8)

The same applies to:

codecvt<char16_t, char, mbstate_t>.
codecvt<char32_t, char, mbstate_t>.
codecvt_utf8.
codecvt_utf16.
codecvt_utf8_utf16.

In fact the header <codecvt> is still not present in the gcc distribution.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
0

You could use utf8cpp (http://utfcpp.sourceforge.net/) it provides those converters as easily useable C++.

schlenk
  • 7,002
  • 1
  • 25
  • 29