Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > wchar_t wstring char string transformations

Reply
Thread Tools

wchar_t wstring char string transformations

 
 
Adrian Cornish
Guest
Posts: n/a
 
      07-12-2003
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
 
Reply With Quote
 
 
 
 
lredmond
Guest
Posts: n/a
 
      07-12-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Adrian Cornish
Guest
Posts: n/a
 
      07-12-2003
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
 
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
converting from windows wchar_t to linux wchar_t yakir22@gmail.com C++ 3 08-15-2008 09:04 AM
ostream_iterator<wstring, wchar_t>(wcout,"\n")) Steven T. Hatton C++ 3 02-02-2006 05:01 AM
wchar_t and wstring with -fshort-wchar optinon with g++. Vinu C++ 1 08-04-2005 03:29 PM
map<wstring, set<wstring> > preserving insertion order? He Shiming C++ 8 01-03-2005 06:42 AM
std::wstring, TCHAR, wchar_t and LPTSTR sorty C++ 4 11-25-2003 11:05 AM



Advertisments