0

I have a string or a char[], but the it is encoded in utf-16, like this:

enter image description here

Now I want to convert it to utf-8 in a new string, Please help me! I already tried like this:

enter image description here

But the compiler tells me I have a problem. How to solve this problem?

peterh
  • 11,875
  • 18
  • 85
  • 108
Jago Li
  • 21
  • 4
  • 3
    it`s better to write a code as a code instead of using a picture. Aslo could you provide us error message that compiler generates ? – Vladimir Jun 02 '16 at 10:14
  • 1
    Besides what @Vlad said, what's `data` in your code? – rubenvb Jun 02 '16 at 12:49
  • Look at this answer http://stackoverflow.com/a/18597384/6345 (C++ Convert string (or char*) to wstring (or wchar_t*)) and adapt to your types if they don't match. – Johann Gerell Jun 02 '16 at 13:18
  • Insert the problem of the compiler into your question. Questions like "it doesn't work" would require some supernatural abilities from us. :-) – peterh Jun 06 '16 at 11:30

1 Answers1

0

The problem is evident: you define u16_str as a std::string when cvt.to_bytes() expect a std::u16string (as the name of the variable suggest).

The following code works for me

#include <locale>
#include <codecvt>
#include <iostream>

int main ()
 {
   std::u16string  u16_str { u"aeiuoàèìòùAEIOU" };

   std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> cvt;

   std::string  u8_str = cvt.to_bytes(u16_str);

   std::cout << u8_str << std::endl;

   return 0;
 }
max66
  • 65,235
  • 10
  • 71
  • 111