468,496 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Convert wstring to UTF-8

I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CTYPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8string, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?

Sep 8 '06 #1
3 6292

Jared Wiltshire wrote:
I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CTYPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8string, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?

The conversion is fairly simple to code yourself. Just remember to
convert to UTF-32 then to UTF-8 and don't convert the individual UTF-16
characters straight to UTF-8. Test it with things like the treble clef
character.
K

Sep 8 '06 #2
Jared Wiltshire schrieb:
I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CTYPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8string, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?
This function has already been written, and it's known in some header of
boost (convert.hpp if I'm not wrong). Ok, it may only be a few lines,
but why bother yourself? Additonally, it will work on any compiler
which knows about utf-8 locales.
-- Markus
Sep 8 '06 #3
So no one knows how to get the wcstombs conversion method to work?

I'll have a look at Boost and see how easy it is to copy a conversion
function out of there anyway...

Thanks.

Markus Grueneis wrote:
Jared Wiltshire schrieb:
I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CTYPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8string, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?

This function has already been written, and it's known in some header of
boost (convert.hpp if I'm not wrong). Ok, it may only be a few lines,
but why bother yourself? Additonally, it will work on any compiler
which knows about utf-8 locales.
-- Markus
Sep 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by gerg | last post: by
8 posts views Thread by Divick | last post: by
10 posts views Thread by v4vijayakumar | last post: by
6 posts views Thread by Jeffrey Walton | last post: by
4 posts views Thread by =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.