I have an array consisting of unicode code points
unsigned short array[3]={0x20ac,0x20ab,0x20ac};
I just want this to be converted as utf-8 to write into file byte by byte using C++.
Example: 0x20ac should be converted to e2 82 ac.
or is there any other method that can directly write unicode characters in file.
This code uses
WideCharToMultiByte
(I assume that you are using Windows):You need to call it twice: first time to get number of output bytes, and second time to actually convert it. If you know output buffer size, you may skip first call. Or, you can simply allocate buffer 2x larger than original + 1 byte (for your case it means 12+1 bytes) - it should be always enough.
The term Unicode refers to a standard for encoding and handling of text. This incorporates encodings like UTF-8, UTF-16, UTF-32, UCS-2, ...
I guess you are programming in a Windows environment, where Unicode typically refers to UTF-16.
When working with Unicode in C++, I would recommend the ICU library.
If you are programming on Windows, don't want to use an external library, and have no constraints regarding platform dependencies, you can use
WideCharToMultiByte
.Example for ICU:
To do exactly what you want:
Finally! With C++11!
Following code may help you,
You could use Boost.Locale of Boost libraries: http://www.boost.org/doc/libs/1_55_0/libs/locale/doc/html/index.html
Iconv is a popular library used on many platforms.