0

I need to convert a UTF-16 string to a UTF-8 string.

const unsigned short *utf16string = ...;
const char *utf8string = convertUTF16toUTF8(utf16string);

Can anybody help me find/write the function const char* convertUTF16toUTF8(const unsigned short* utf16string)?

The types can't be changed, unfortunately, and I don't have access to C++11. I've been beating my head at this for hours and can't find any solutions.

Please help me. I believe in you, StackOverflow!

P.S. If anybody needs to know, I'm making a game using cocos2d-x and I'm trying to get a string using the JNI callbacks. JNI has a UTF version of the GetString... functions, yet these are buggy according to my own experience and Wikipedia, so I need to convert the strings using the raw functions.

Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • What environment are you in? There might already be a function in the OS. But otherwise I'd just read in one code-point at a time from the UTF-16 string - potentially over two shorts - and then write it out as UTF-8 chars, then repeat. To allocate the buffer ahead of time you can probably guess it'll be the same size as the UTF-16 or shorter and then reallocate if you hit the limit. – Rup Jan 16 '14 at 17:46
  • This code is being used on Android. – Guy Kogus Jan 16 '14 at 17:56
  • As I mentioned in the question, I don't have access to it :( – Guy Kogus Jan 16 '14 at 18:09
  • Is this question related to your problem? Does it help? - http://stackoverflow.com/questions/2867123/convert-utf-16-to-utf-8-under-windows-and-linux-in-c – David G Jan 16 '14 at 18:31
  • http://utfcpp.sourceforge.net/ – dalle Jan 16 '14 at 21:02
  • @dalle you did it! You genius! Post that link as the answer and I'll mark it for you. – Guy Kogus Jan 20 '14 at 10:04
  • [wcstombs](http://en.cppreference.com/w/cpp/string/multibyte/wcstombs) – David Heffernan Jan 20 '14 at 14:50

1 Answers1

2

If you want a simple and small C++ library you could use UTF8-CPP to convert between UTF-8 and UTF-16.

dalle
  • 18,057
  • 5
  • 57
  • 81