473,387 Members | 1,619 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,387 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 64076
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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,...

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.