wchar_t wstring char string transformations

Discussion in 'C++' started by Adrian Cornish, Jul 12, 2003.

  1. Hi all,

    Is there a portable way of transforming a wchar_t to a char and/or
    wstring to a string.

    Are there any gurantees for the layout of a wchar_t, like every other
    byte is a char?

    I am not worried about data loss.

    Also is the output of this program guranteed to work as I expect. Its
    seems fine on g++ but C++ Builder 6 does not output anything to wcout.
    Is this a compiler issue?

    This is what I expect:-

    And the bit in brackets [have been sent to wcout] instead of cout
    And the bit in brackets [have been sent to wcout] instead of cout
    And the bit in brackets [have been sent to wcout] instead of cout

    and C++ Builder give me:-

    And the bit in brackets [] instead of cout
    And the bit in brackets [] instead of cout
    And the bit in brackets [] instead of cout

    #include <iostream>

    int main()
    {
    wchar_t *wptr=L"has been sent to wcout";
    std::wstring wstr(wptr);

    std::cout << "And the bit in brackets [";
    std::wcout << wptr;
    std::cout << "] instead of cout" << std::endl;

    std::cout << "And the bit in brackets [";
    std::wcout << wstr;
    std::cout << "] instead of cout" << std::endl;

    std::cout << "And the bit in brackets [";
    std::wcout << wstr.c_str();
    std::cout << "] instead of cout" << std::endl;
    return 0;
    }


    --
    Adrian Cornish

    BlueDreamer Ltd
    Phone: 0208 506 1226
    Mobile: 07968 062 926
    Website: www.bluedreamer.com
    Adrian Cornish, Jul 12, 2003
    #1
    1. Advertising

  2. Adrian Cornish

    lredmond Guest

    there is a function mbstowcs, this converts a multi nte string to a wide
    character string and wcstombs does the reverse. You will need to include
    stdlib.h or cstdlib

    "Adrian Cornish" <> wrote in message
    news:...
    > Hi all,
    >
    > Is there a portable way of transforming a wchar_t to a char and/or
    > wstring to a string.
    >
    > Are there any gurantees for the layout of a wchar_t, like every other
    > byte is a char?
    >
    > I am not worried about data loss.
    >
    > Also is the output of this program guranteed to work as I expect. Its
    > seems fine on g++ but C++ Builder 6 does not output anything to wcout.
    > Is this a compiler issue?
    >
    > This is what I expect:-
    >
    > And the bit in brackets [have been sent to wcout] instead of cout
    > And the bit in brackets [have been sent to wcout] instead of cout
    > And the bit in brackets [have been sent to wcout] instead of cout
    >
    > and C++ Builder give me:-
    >
    > And the bit in brackets [] instead of cout
    > And the bit in brackets [] instead of cout
    > And the bit in brackets [] instead of cout
    >
    > #include <iostream>
    >
    > int main()
    > {
    > wchar_t *wptr=L"has been sent to wcout";
    > std::wstring wstr(wptr);
    >
    > std::cout << "And the bit in brackets [";
    > std::wcout << wptr;
    > std::cout << "] instead of cout" << std::endl;
    >
    > std::cout << "And the bit in brackets [";
    > std::wcout << wstr;
    > std::cout << "] instead of cout" << std::endl;
    >
    > std::cout << "And the bit in brackets [";
    > std::wcout << wstr.c_str();
    > std::cout << "] instead of cout" << std::endl;
    > return 0;
    > }
    >
    >
    > --
    > Adrian Cornish
    >
    > BlueDreamer Ltd
    > Phone: 0208 506 1226
    > Mobile: 07968 062 926
    > Website: www.bluedreamer.com
    lredmond, Jul 12, 2003
    #2
    1. Advertising

  3. lredmond wrote:
    >
    > there is a function mbstowcs, this converts a multi nte string to a wide
    > character string and wcstombs does the reverse. You will need to include
    > stdlib.h or cstdlib


    Thanks, just what I was looking for.

    --
    Adrian Cornish

    BlueDreamer Ltd
    Phone: 0208 506 1226
    Mobile: 07968 062 926
    Website: www.bluedreamer.com
    Adrian Cornish, Jul 12, 2003
    #3
    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. sorty
    Replies:
    4
    Views:
    20,725
    Rolf Magnus
    Nov 25, 2003
  2. He Shiming
    Replies:
    8
    Views:
    4,825
    Stephen Howe
    Jan 3, 2005
  3. Vinu
    Replies:
    1
    Views:
    1,433
    msalters
    Aug 4, 2005
  4. Steven T. Hatton
    Replies:
    3
    Views:
    1,269
    Steven T. Hatton
    Feb 2, 2006
  5. Replies:
    3
    Views:
    1,100
    James Kanze
    Aug 15, 2008
Loading...

Share This Page