How to convert UTF-8 to wchar_t (and back)

While this seems like re-inventing the wheel, it sure is hard to find this code for free on-line (believe me, I tried 😉 ).
Of course, you might wanna use iconv for this, but it’s a bit too huge if all you want is UTF-8 <-> wchar_t.

Fortunately, Alexey Vatchenko has made the code available under BSD license, with a simple and clean interface.

Note: This code is converting UTF-8 to UCS-4-internal, thus it works properly only on systems, where size of wchar_t is 4 bytes (Linux/Unix, BSD, MacOS), but not on Windows, where wchar_t is only 2 bytes!

Happy coding!

One response to “How to convert UTF-8 to wchar_t (and back)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s