Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Convert wstring to UTF-8

Reply
Thread Tools

Convert wstring to UTF-8

 
 
Jared Wiltshire
Guest
Posts: n/a
 
      09-08-2006
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?

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a
 
      09-08-2006

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

 
Reply With Quote
 
 
 
 
Markus Grueneis
Guest
Posts: n/a
 
      09-08-2006
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
 
Reply With Quote
 
Jared Wiltshire
Guest
Posts: n/a
 
      09-09-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert XMLCh* to std::wstring? Meal XML 3 07-20-2007 03:28 AM
Q: Convert std::string to std::wstring using std::ctype widen() Jeffrey Walton C++ 10 11-26-2006 02:37 PM
how to convert wstring to 64 bits? Simon Guertin Windows 64bit 2 05-06-2006 11:24 AM
STL: how to convert wstring to string gerg C++ 6 12-07-2005 03:53 PM
map<wstring, set<wstring> > preserving insertion order? He Shiming C++ 8 01-03-2005 06:42 AM



Advertisments