473,406 Members | 2,707 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,406 software developers and data experts.

STL: how to convert wstring to string

How would one convert a wstring to a string?

This is what I have so far:

bool copyto(std::string& l,std::wstring& r)
{
bool ret = false;

size_t i = 0;
const size_t n = r.length()+1;
l.resize(n); // make sure we have enough
for(;i<n;++i)
{
l[i] = r[i];
}
l[i] = 0;
l.resize(n-1);

return ret;
}

which I know is pretty gruesome, but does the job.

the reason for: const size_t n = r.length()+1;
is because I want to make sure I have enough room to transfer the
characters.

Nov 29 '05 #1
6 64081
gerg <ja*****@gmail.com> wrote:
| How would one convert a wstring to a string?

I'm not sure whether or not this is portable, but on many systems one
could, given the wstring containing only characters in the range of
char, do an elementwise copy.

| This is what I have so far:
|
| bool copyto(std::string& l,std::wstring& r)
| {
| bool ret = false;
|
| size_t i = 0;
| const size_t n = r.length()+1;

this is one too many.

| l.resize(n); // make sure we have enough

we don't need the extra byte.

| for(;i<n;++i)
| {
| l[i] = r[i];
| }
| l[i] = 0;

strings are not zero terminated.

| l.resize(n-1);

And here we are removing the last character again.

| return ret;
| }
|
| which I know is pretty gruesome, but does the job.

It would be somewhat simpler if you would not pretend you needed the
extra zero. If that is needed, the string class would take care of the
job.

| the reason for: const size_t n = r.length()+1;
| is because I want to make sure I have enough room to transfer the
| characters.

No, it's for making sure you have enough room to transfer an extra
character too. Anyhow, you are reinventing the weel. The constructor
and some member functions of string takes iterators of different types
as parameters. The code below demonstrates some.

wstring ws = L"Hello";
string s(ws.begin(), ws.end());
s.assign(ws.begin(), ws.end());

--
Robert Bauck Hamar
Nov 29 '05 #2
>> wstring ws = L"Hello";
string s(ws.begin(), ws.end());
s.assign(ws.begin(), ws.end());

sweet, thanks

-greg

Nov 29 '05 #3
example doesn't compile. i didn't realize that string wasn't null
terminated! OK thanks.

Nov 29 '05 #4
ro**********@ifi.uio.no wrote:

wstring ws = L"Hello";
string s(ws.begin(), ws.end());
s.assign(ws.begin(), ws.end());


Generally, It works but it doesn't take codepage/charset into
consideration.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Nov 30 '05 #5
Mateusz Loskot <ma*****@loskot.net> wrote:
| ro**********@ifi.uio.no wrote:
| >
| > wstring ws = L"Hello";
| > string s(ws.begin(), ws.end());
| > s.assign(ws.begin(), ws.end());
|
| Generally, It works but it doesn't take codepage/charset into
| consideration.

<quote>
I'm not sure whether or not this is portable, but on many systems one
could, given the wstring containing only characters in the range of
char, do an elementwise copy.
</quote>

That's what my disclaimer is all about.

§ 2.2.3 guarantees that char and wchar_t has some characters in common.
(A-Z, a-z, some whitespace characters, some punctuation characters, and
some control characters.) The specific values are
implementation-defined. That means that your compiler must document
whether or not this should work.
--
Robert Bauck Hamar
Dec 7 '05 #6

ro**********@ifi.uio.no wrote:
Mateusz Loskot <ma*****@loskot.net> wrote:
| ro**********@ifi.uio.no wrote:
| >
| > wstring ws = L"Hello";
| > string s(ws.begin(), ws.end());
| > s.assign(ws.begin(), ws.end());
|
| Generally, It works but it doesn't take codepage/charset into
| consideration.

<quote>
I'm not sure whether or not this is portable, but on many systems one
could, given the wstring containing only characters in the range of
char, do an elementwise copy.
</quote>

That's what my disclaimer is all about.

§ 2.2.3 guarantees that char and wchar_t has some characters in common.
(A-Z, a-z, some whitespace characters, some punctuation characters, and
some control characters.) The specific values are
implementation-defined. That means that your compiler must document
whether or not this should work.


Well, your original disclaimer didn't exacly mention that even if
wchar_t can hold a superset of char it doesn't imply that the same
character
must have the same value. Furthermore, even your second post isn't
complete.
The compiler doesn't have to document whether the other char values
also
match wchar_t values. This is rarely the case. On systems where wchar_t
is Unicode and char is ISO-8859-1, this is true. On systems where
wchar_t
is Unicode and char is NOT ISO-8859-1, it's not true. Since Europe
needs ISO-8859-15 nowadays, and Windows uses CP1252 for char, don't
count on it. Just try the euro sign (U+20AC)

HTH,
Michiel Salters

Dec 7 '05 #7

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

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
1
by: Tao | last post by:
hi.. Group, is there anyone know how to convert wstring to BSTR? thanks.
3
by: timor.super | last post by:
Hi group, how to convert a string to a vector of unsigned char ? I used to iterate trough the string to set the vector, but I think this is not the best way to do this. I'm a beginner with the...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
1
by: ycinar | last post by:
How can I convert a string (or a wstring) to a PWCHAR? PWCHAR temp; wstring wstr; // Basically I want to do // temp = wstr // but casting didnt work // temp = PWCHAR (wstr) // error...
12
by: aatif | last post by:
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string. hex string: 5000005355.... ASCII:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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.