Convert wstring to UTF-8

Discussion in 'C++' started by Jared Wiltshire, Sep 8, 2006.

  1. 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?
     
    Jared Wiltshire, Sep 8, 2006
    #1
    1. Advertising

  2. 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
     
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Sep 8, 2006
    #2
    1. Advertising

  3. 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
     
    Markus Grueneis, Sep 8, 2006
    #3
  4. 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
     
    Jared Wiltshire, Sep 9, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. He Shiming
    Replies:
    8
    Views:
    4,843
    Stephen Howe
    Jan 3, 2005
  2. gerg
    Replies:
    6
    Views:
    17,037
  3. Jeffrey Walton
    Replies:
    10
    Views:
    940
    Mathias Gaunard
    Nov 26, 2006
  4. Meal
    Replies:
    3
    Views:
    1,183
    Joe Kesselman
    Jul 20, 2007
  5. Peter Poulsen
    Replies:
    5
    Views:
    706
Loading...

Share This Page