UTF16 to UTF8 to UTF16 simple CString based conversion

Introduction

For conversion of strings between UTF8 and UTF16 (as well as other formats), Microsoft gives us the MultiByteToWideChar and WideCharToMultiByte functions. These functions use null terminated char/widechar based strings. Use of those strings requires a bit of memory management, and if you use the functions extensively, your code may end up looking like a complete mess. That's why I decided to wrap these two functions for use with the more coder-friendly CString types.

Using the code

Use of the two helper functions is straightforward. But, do note that they are only useful if your project is set to use the UNICODE character set. The functions also only work in Visual Studio 7.1 or above. If you use Visual Studio 6.0, you won't be able to compile because you miss CStringA and CStringW. In the following code snippet, you have a usage example:

Also passing -1 as length to MultiByteToWideChar makes it include the terminating 0 in the process. So no need to add 1 to len, and also no need to zero out the buffer.
Exactly the same thing can be done for the UTF16 ot UTF8 translation.

The function provided here seems to have one defect for decoding the characters with mixed encoding. And sometimes it cuts the last character for some strings. The new version just didn't work for me apart from one aparent bug.