473,414 Members | 1,697 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,414 software developers and data experts.

changing char to wstring, how?

How can I conver a char[x] to std::wstring, for intance
time_t Time_t;
std::wstring Date;
time( &Time_t );
Date = ctime( &Time_t );

or
strd::string wstr;
str = __FILE__;

Isn't the conversion done automatically?
I am using g++ 3.3.x
TIA

Oct 6 '05 #1
6 14998
On 6 Oct 2005 12:06:27 -0700, "jalkadir" <ja******@gosonic.ca> wrote:
How can I conver a char[x] to std::wstring, for intance
time_t Time_t;
std::wstring Date;
time( &Time_t );
Date = ctime( &Time_t );


You can use mbstowcs (better write a C++ wrapper):
http://msdn.microsoft.com/library/de...t_mbstowcs.asp

Best wishes,
Roland Pibinger
Oct 6 '05 #2
Thanks for the prompt response Roland.
I read the http site and it does exactly what I needed, however, I
wonder if there is pure C++ way to do this, do you know?

Thanks again.

Oct 6 '05 #3
My test run compiles, but I get a segmentation fault.

gdb.exe reports
#0 0x77c1d2b9 in msvcrt!mblen
What am I doing wrong?
Any body!!

#include <cstdlib>
#include <iostream>
#include <wchar.h>

using namespace std;
std::wstring ctow(const char* src){
wchar_t* dest;
int i = mbstowcs(dest, src, sizeof(src));
return dest;
}
int main(int argc, char *argv[])
{
const char* str = "hola";
std::wstring wstr;
wstr = ctow(str);

system("PAUSE");
return EXIT_SUCCESS;
}

Oct 6 '05 #4

"jalkadir" <ja******@gosonic.ca> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
My test run compiles, but I get a segmentation fault.

gdb.exe reports
#0 0x77c1d2b9 in msvcrt!mblen
What am I doing wrong?
Any body!!

#include <cstdlib>
#include <iostream>
#include <wchar.h>

using namespace std;
std::wstring ctow(const char* src){
wchar_t* dest;
int i = mbstowcs(dest, src, sizeof(src));


'dest' is not pointing anywhere.

-Mike
Oct 6 '05 #5
jalkadir wrote:
My test run compiles, but I get a segmentation fault.

gdb.exe reports
#0 0x77c1d2b9 in msvcrt!mblen
What am I doing wrong?
Any body!!
You don't understand pointers and arrays. You need to C++ text book.

#include <cstdlib>
#include <iostream>
#include <wchar.h>

using namespace std;
std::wstring ctow(const char* src){
wchar_t* dest;
Unitinialised pointer, you need to allocate some memory.
int i = mbstowcs(dest, src, sizeof(src));


sizeof(src) gives the sizeof the pointer (four bytes) not the size of
the string.

strlen(src) seems right.

It would be better like this, using a vector to allocate the necessary
memory.

std::wstring ctow(const char* src){
std::vector<wchar_t> dest(how_ever_many_bytes_you_need);
int i = mbstowcs(&dest[0], src, strlen(src));
return std::wstring(&dest[0]);
}

I don't know how you get the value of 'how_ever_many_bytes_you_need',
I'll leave you to work that out.

If you are a newbie, then don't use pointers when you don't have to,
especially when you don't understand them.

john
Oct 7 '05 #6
Thanks!! it did the trick, but I still have a lot of questions about
IOStreams, regarding wide stream.

I'll keep posting.

Again, thanks!

Oct 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adrian Cornish | last post by:
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...
1
by: Saurabh Aggrawal | last post by:
Hi, On line nos. 24, 25, 26 24: wstring cfMethods = {{L"setLabel"},{L""}}; 25: wstring cfProperties= {{L"isVisible"},{L""}}; 26: wstring cfEvents =...
20
by: Peteroid | last post by:
How the heck does one convert a String* to char? Specifically: String* my_string = "HELLO" ; char m_char_array ; m_char_array = my_string ; // error strcpy( m_char_array, my_string ) ; //...
8
by: Frank R Eid | last post by:
Hi all; A) What's the simplest way to convert a 'String' to 'char *' (and the other way araound) ? B) I have some C++ classes that I cannot easily convert to .NET (because they still are...
3
by: uday.sen | last post by:
Hi, I am porting a piece of code from VC++ to linux platform. My compiler is g++ 3.2.2. I am getting following error: no matching function for call to A::setResponse(std::wstring) candidates...
8
by: Divick | last post by:
Hi all, can somebody tell how much std::wstring is supported across different compilers on different platforms? AFAIK std::string is supported by almost all C++ compilers and almost all platforms,...
9
by: Mwob | last post by:
Hi, I have a very simple function, declared like this: wstring CMyClass::GetDate() { .... wcsftime (sDateString,255,L"%d-%b-%Y",&dTmpDate); wstring sText(sDateString); return sText;
10
by: v4vijayakumar | last post by:
1. why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str()...
8
bajajv
by: bajajv | last post by:
Hi, I have to add one string and one integer in an existing string. eg. int32 number and wstring Title to an existing wstring. how can this be done?
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.