473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

wchar_t wstring char string transformations

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
2 24065
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Julius Mong | last post by:
Hi all, I'm doing this: // Test char code wchar_t lookup = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01}; for (int x=0; x<5; x++) { wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t)); string =...
1
by: Vinu | last post by:
Hi I have one problem. I compiled my program with -fshort-wchar option using g++. In my program I am using wstring also. In some places I converted the wchar_t variable to wstring . wstring...
3
by: gamehack | last post by:
Hi all, I was doing a bit of research about writing yet another build tool but that's not the point of my mail. I'm going to ask a few questions about how to resolve a few internationalization...
3
by: Steven T. Hatton | last post by:
There's probably something obvious I'm missing here, but I can't seem to figure out how to get this to work: ostream_iterator<wstring, wchar_t>(wcout,"\n")); When I try to compile it, I get an...
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
22
by: Albert Oppenheimer | last post by:
I thought my program had to be caught in a loop, and cancelled it through the task manager. It took about one second in Java, but re-implemented in C, it had already run over one minute. I set...
3
by: Angus | last post by:
I can see how to get a char* but is it possible to get a wide char - eg wchar_t?
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
4
by: =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post by:
Hi, I have an API that returns UTF-8 encoded strings. I have a utf8 codevt facet available to do the conversion from UTF-8 to wchar_t encoding defined by the platform. I have no trouble...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.