String contains utf16... but I want utf8

Hi everybody,
I have a string which contains a compressed string.
I use zlib to deflate it, but it returns a string not a wstring.
In the result, I obtain a XML encoded in UTF16, so how can I convert my string to a "readable" string? The result in c++ string type is : \0<\0?\0X\0M\0L..... it seems really to a wstring type...

I searched for a long time on google, but didn't found how to do it...

Thanks by advance,

(after this work, I would change information in the XML, then I will encode it in UTF16 string to inflate it at the end... ;) )
To convert a valid UTF-16 buffer to UTF-8 use WideCharToMultiByte function, use CP_UTF8 as first parameter.

Zlib library returns a buffer, not a string or wstring, keep it that way. You should be able to know the length in advance in case it is not null terminated.
I use zlib to deflate it, but it returns a string not a wstring.
As modoran has already said, the zlib functions are using char* parameters to return (generic) byte-wise data, not actual string (just like the socket API's send(), recv(), etc. -- in case than helps.)

So if you know the data you've extracted is a UTF-16 encoded string, you can safely case the buffer to a wchar_t* for processing (using WideCharToMultiByte or whatever.)


PS Out of interest (nosiness?), why do you need UTF-8. The Windows API works natively with UTF-16, so I assume you have some other reason to use UTF-8 instead.

Last edited on
Thanks for your answers, it was the good way !
Topic archived. No new replies allowed.