473,941 Members | 2,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::str ing& 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 64184
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::str ing& 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.beg in(), ws.end());

--
Robert Bauck Hamar
Nov 29 '05 #2
>> wstring ws = L"Hello";
string s(ws.begin(), ws.end());
s.assign(ws.beg in(), 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**********@if i.uio.no wrote:

wstring ws = L"Hello";
string s(ws.begin(), ws.end());
s.assign(ws.beg in(), 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**********@if i.uio.no wrote:
| >
| > wstring ws = L"Hello";
| > string s(ws.begin(), ws.end());
| > s.assign(ws.beg in(), 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**********@if i.uio.no wrote:
Mateusz Loskot <ma*****@loskot .net> wrote:
| ro**********@if i.uio.no wrote:
| >
| > wstring ws = L"Hello";
| > string s(ws.begin(), ws.end());
| > s.assign(ws.beg in(), 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
10316
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
4
17699
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
15
10859
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
9215
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
369
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, « '1' + 1 » is equal to « '11' »: the String deciding...
1
9932
by: Tao | last post by:
hi.. Group, is there anyone know how to convert wstring to BSTR? thanks.
3
40768
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 STL How about allocating the vector ? should I know before the size ? And I would like to do then opposite conversion, from an unsigned char
2
2245
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? ----------------------------------------------------------------------- Javascript variables are loosely typed: the conversion between a string and a number happens automatically. Since plus (+) is also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the String deciding...
1
8378
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 C2440: 'type cast' : cannot convert from 'std::wstring' to 'PWCHAR'
12
5010
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: P<null><null>SU... I can do it and the string length also includes nulls but when I concatenate other string, it doesn't show as its part. string HexValue = "500000535500";
0
10134
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9964
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11113
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10659
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6080
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.