Convert wstring to UTF-8

  • Thread starter Jared Wiltshire
  • Start date
J

Jared Wiltshire

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?
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Jared said:
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
 
M

Markus Grueneis

Jared said:
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
 
J

Jared Wiltshire

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 said:
Jared said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top