wchar_t wstring char string transformations

A

Adrian Cornish

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
 
L

lredmond

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
 
A

Adrian Cornish

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

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top