By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

wchar_t wstring char string transformations

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <nn**@bluedreamer.com> wrote in message
news:3F***************@bluedreamer.com...
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

Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.