By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

wide string problem ...

P: n/a
Hi

I'm having major problems writing a function that converts a long
value into a wide string !!! I'm more than likely going round in
circles and not seeing the wood for the trees now but am frustreated
just the same.

My function looks like this:

std::wstring CReportCreator::SetEmpricalDefaults(const udtLONG&
udtValue)
{
wstring ret = L"";
ret = Utilities::LongToStdWString(udtValue.lngValue);
return ret;

}
LongToStdWString() is used elsewhere and works ok.

My call is this:

wstring strOut = SetEmpricalDefaults(300L);

and then:

WCHAR xml[255];
wsprintf(xml, L"<X><a=\"%s\" /></X>", strOut);

wsprintf throws a scalar deleting desctructor exception.

when i debug strOut show as "*300".

does anyone know what the '*' represents at the beginning of the 300 ?

does anyone know of a function that will return a unicode string from
a long ? so far i've tried stringstreams and _ltow but always get the
* in front of the 300 and wsprintf throws an exception ...

many thanks

Mar 12 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
On Mar 12, 5:27 pm, "SteveB" <s...@thebretts.co.ukwrote:
Hi

I'm having major problems writing a function that converts a long
value into a wide string !!! I'm more than likely going round in
circles and not seeing the wood for the trees now but am frustreated
just the same.

My function looks like this:

std::wstring CReportCreator::SetEmpricalDefaults(const udtLONG&
udtValue)
{
wstring ret = L"";
ret = Utilities::LongToStdWString(udtValue.lngValue);
return ret;

}

LongToStdWString() is used elsewhere and works ok.

My call is this:

wstring strOut = SetEmpricalDefaults(300L);

and then:

WCHAR xml[255];
wsprintf(xml, L"<X><a=\"%s\" /></X>", strOut);
wsprintf(xml, L"<X><a=\"%s\" /></X>", strOut.data()); //Should be
this?
>
wsprintf throws a scalar deleting desctructor exception.

when i debug strOut show as "*300".

does anyone know what the '*' represents at the beginning of the 300 ?

does anyone know of a function that will return a unicode string from
a long ? so far i've tried stringstreams and _ltow but always get the
* in front of the 300 and wsprintf throws an exception ...

many thanks

Mar 12 '07 #2

P: n/a
* SteveB:
Hi

I'm having major problems writing a function that converts a long
value into a wide string !!! I'm more than likely going round in
circles and not seeing the wood for the trees now but am frustreated
just the same.

My function looks like this:

std::wstring CReportCreator::SetEmpricalDefaults(const udtLONG&
udtValue)
{
wstring ret = L"";
ret = Utilities::LongToStdWString(udtValue.lngValue);
return ret;

}

Just a style issue, but make that

{
return Utilities::LongToStdWString(udtValue.lngValue);
}
LongToStdWString() is used elsewhere and works ok.
If you say so.

My call is this:

wstring strOut = SetEmpricalDefaults(300L);

and then:

WCHAR xml[255];
wsprintf(xml, L"<X><a=\"%s\" /></X>", strOut);

wsprintf throws a scalar deleting desctructor exception.
Yes, passing a non-POD class type object to "..." is Undefined Behavior.

It's unclear why you're trying to copy the string to a fixed size buffer.

when i debug strOut show as "*300".

does anyone know what the '*' represents at the beginning of the 300 ?
Depends on your debugger if it's not part of the textual string value.
does anyone know of a function that will return a unicode string from
a long ?
You stated above, "LongToStdWString() is used elsewhere and works ok".
so far i've tried stringstreams
The wide string stream support in g++ for Windows is lacking; perhaps
that's the compiler you're using?

and _ltow
Non-standard.

but always get the * in front of the 300
Mostly irrelevant.

and wsprintf throws an exception ...
If this is from the code shown above, then see above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 12 '07 #3

P: n/a
On 12 Mar, 09:59, "mimi" <cainiaodelixi...@gmail.comwrote:
On Mar 12, 5:27 pm, "SteveB" <s...@thebretts.co.ukwrote:
Hi
I'm having major problems writing a function that converts a long
value into a wide string !!! I'm more than likely going round in
circles and not seeing the wood for the trees now but am frustreated
just the same.
My function looks like this:
std::wstring CReportCreator::SetEmpricalDefaults(const udtLONG&
udtValue)
{
wstring ret = L"";
ret = Utilities::LongToStdWString(udtValue.lngValue);
return ret;
}
LongToStdWString() is used elsewhere and works ok.
My call is this:
wstring strOut = SetEmpricalDefaults(300L);
and then:
WCHAR xml[255];
wsprintf(xml, L"<X><a=\"%s\" /></X>", strOut);

wsprintf(xml, L"<X><a=\"%s\" /></X>", strOut.data()); //Should be
this?


wsprintf throws a scalar deleting desctructor exception.
when i debug strOut show as "*300".
does anyone know what the '*' represents at the beginning of the 300 ?
does anyone know of a function that will return a unicode string from
a long ? so far i've tried stringstreams and _ltow but always get the
* in front of the 300 and wsprintf throws an exception ...
many thanks- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
many thanks

Mar 12 '07 #4

P: n/a
Alf I'm sure you're a clever guy but I really just asked a question.
The one line answer above was just fine.
Mar 13 '07 #5

P: n/a
* SteveB:
Alf I'm sure you're a clever guy but I really just asked a question.
The one line answer above was just fine.
Please quote relevant context.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 13 '07 #6

P: n/a
* Alf P. Steinbach:
* SteveB:
>Alf I'm sure you're a clever guy but I really just asked a question.
The one line answer above was just fine.

Please quote relevant context.
I mean, I'm not into guessing games: if you want help, be clear.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 13 '07 #7

P: n/a
On 13 Mar, 14:08, "Alf P. Steinbach" <a...@start.nowrote:
* Alf P. Steinbach:
* SteveB:
Alf I'm sure you're a clever guy but I really just asked a question.
The one line answer above was just fine.
Please quote relevant context.

I mean, I'm not into guessing games: if you want help, be clear.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
i wasn't posting to get a lecture in how to phrase my questions alf.

it was clear enough for someone else to aswer.

i think you need to get over yourself.

Mar 16 '07 #8

P: n/a
On 16 Mar., 13:39, "SteveB" <s...@thebretts.co.ukwrote:
i think you need to get over yourself.
And I think you need to learn to behave yourself. Alf gave you lots of
valuable info. Being rude is about the silliest response imaginable.

/Peter

Mar 16 '07 #9

P: n/a
On 16 Mar, 13:08, "peter koch" <peter.koch.lar...@gmail.comwrote:
On 16 Mar., 13:39, "SteveB" <s...@thebretts.co.ukwrote:i think you need to get over yourself.

And I think you need to learn to behave yourself. Alf gave you lots of
valuable info. Being rude is about the silliest response imaginable.

/Peter
i wasn't being rude peter. mimi answered my question without needing
to dismantle the post and split hairs. my post was directed at the
community and was just at alf. if you read the reply he doesn't really
contribute anything at all. all i'm saying is that if he dopesn't know
the answer where is the benefit of his one hair splitters ? and the be
honest i did look at a few of alfs other posts before i commented.

Mar 16 '07 #10

P: n/a
On 16 Mar., 14:56, "SteveB" <s...@thebretts.co.ukwrote:
On 16 Mar, 13:08, "peter koch" <peter.koch.lar...@gmail.comwrote:
On 16 Mar., 13:39, "SteveB" <s...@thebretts.co.ukwrote:i think you need to get over yourself.
And I think you need to learn to behave yourself. Alf gave you lots of
valuable info. Being rude is about the silliest response imaginable.
/Peter

i wasn't being rude peter.
You think not? I doubt many would agree with you here.
mimi answered my question without needing
to dismantle the post and split hairs.
mimi gave you the wrong answer .data is not useful for the printf
family.
my post was directed at the
community and was just at alf. if you read the reply he doesn't really
contribute anything at all.
Alf guided you to allow you to make better code, he told you about a
possible compiler error, he told you about non-portability of your
code, he told you why your code does not work, and he did guide you to
find out about the "*" showing up in the debugger.
How can that not be useful?
all i'm saying is that if he dopesn't know
the answer where is the benefit of his one hair splitters ?
Hair splitters? There was no hairsplitting at all.
and the be
honest i did look at a few of alfs other posts before i commented.
If you had any intelligence, you'd then be aware that Alf answers are
knowledgeable and helpful. I can't remember ever seeing bad advice
from Alf, so if there is any it must be quite rare.
Now, I'd like to be able to put you in my killfile, but as I post via
google this is not an option.

/Peter

Mar 16 '07 #11

P: n/a
* SteveB:
On 13 Mar, 14:08, "Alf P. Steinbach" <a...@start.nowrote:
>* Alf P. Steinbach:
>>* SteveB:
Alf I'm sure you're a clever guy but I really just asked a question.
The one line answer above was just fine.
Please quote relevant context.
I mean, I'm not into guessing games: if you want help, be clear.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

i wasn't posting to get a lecture in how to phrase my questions alf.

it was clear enough for someone else to aswer.

i think you need to get over yourself.
Don't quote signatures, please.

Why are you trying to post negative drivel about me?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 16 '07 #12

P: n/a
Alf

I wasn't posting negative drivel about you - I appreciate that your
intentions were honourable and you wanted to guide me to making a
better choice - but not all people that post want this kind of advice
- i had a specific problem and i posted it on here to get a specific
answer. when i read your post it read as being condescending and not
very helpful. i was on a tight deadline and needed to make some
progress - i had tried google and other sources first. let's put this
to bed eh ?

Peter.
does anyone know what the '*' represents at the beginning of the 300 ?
Depends on your debugger if it's not part of the textual string value.

Read the post again Peter as Alf tells me very little.

Perhaps you could tell me why the .data() function is no good with
wsprintf ? I can't find anything on google or these groups that
mentions any problems and the code works ok ?

Belive me chaps I have no wish to fall out with anyone here - I find
these groups help me a lot. I had a gripe and now I've got it off my
chest.
Steve

Mar 16 '07 #13

P: n/a
On 16 Mar., 23:12, "SteveB" <s...@thebretts.co.ukwrote:
Alf

I wasn't posting negative drivel about you - I appreciate that your
intentions were honourable and you wanted to guide me to making a
better choice - but not all people that post want this kind of advice
- i had a specific problem and i posted it on here to get a specific
answer. when i read your post it read as being condescending and not
very helpful. i was on a tight deadline and needed to make some
progress - i had tried google and other sources first. let's put this
to bed eh ?
The questions and answers here are not just for you. They are also
meant as a source of information for others with similar problems.
>
Peter.
does anyone know what the '*' represents at the beginning of the 300 ?

Depends on your debugger if it's not part of the textual string value.

Read the post again Peter as Alf tells me very little.
On the contrary. Alf explains that you should check if the '*' is part
of the textual string. If it is not, you should consult your debugger
manual and find out why a '*' might be prepended. This last part is
off-topic in this group.
>
Perhaps you could tell me why the .data() function is no good with
wsprintf ? I can't find anything on google or these groups that
mentions any problems and the code works ok ?
Well - it is already there in the documentation of string::data and
the printf family. Read the documentation and you will have your
answer.

/Peter

/Peter

Mar 17 '07 #14

P: n/a
Peter
The questions and answers here are not just for you. They are also
meant as a source of information for others with similar problems.
see comment below.
does anyone know what the '*' represents at the beginning of the 300 ?
Depends on your debugger if it's not part of the textual string value.
Read the post again Peter as Alf tells me very little.

On the contrary. Alf explains that you should check if the '*' is part
of the textual string. If it is not, you should consult your debugger
manual and find out why a '*' might be prepended. This last part is
off-topic in this group.
Perhaps you could tell me why the .data() function is no good with
wsprintf ? I can't find anything on google or these groups that
mentions any problems and the code works ok ?

Well - it is already there in the documentation of string::data and
the printf family. Read the documentation and you will have your
answer.
In my original post I pointed out that I was converting long numbers
to wide strings and that was the problem. The '*' is not part of the
original string. How could it be ?

In reply to your final point I can find nothing on Google pointing to
problems with wstring::data and wsprintf. If I had the time to readl
all the documentation I really wouldn't have to post any questions on
here at all !

Please indulge me and explain the problem you claim to be so obvious.

Mar 17 '07 #15

P: n/a
SteveB wrote:
Peter
>The questions and answers here are not just for you. They are also
meant as a source of information for others with similar problems.

see comment below.
does anyone know what the '*' represents at the beginning of the 300
?
Depends on your debugger if it's not part of the textual string value.
Read the post again Peter as Alf tells me very little.

On the contrary. Alf explains that you should check if the '*' is part
of the textual string. If it is not, you should consult your debugger
manual and find out why a '*' might be prepended. This last part is
off-topic in this group.
Perhaps you could tell me why the .data() function is no good with
wsprintf ? I can't find anything on google or these groups that
mentions any problems and the code works ok ?

Well - it is already there in the documentation of string::data and
the printf family. Read the documentation and you will have your
answer.

In my original post I pointed out that I was converting long numbers
to wide strings and that was the problem. The '*' is not part of the
original string. How could it be ?

In reply to your final point I can find nothing on Google pointing to
problems with wstring::data and wsprintf. If I had the time to readl
all the documentation I really wouldn't have to post any questions on
here at all !

Please indulge me and explain the problem you claim to be so obvious.
Sorry for jumping in: data() is not guaranteed to be 0-terminated. The
c_str() method is. The reason that you find the code working is that both
methods return the same pointer on your library implementation. In fact, I
don't know of any implementation that does otherwise.
Best

Kai-Uwe Bux
Mar 17 '07 #16

P: n/a
On 17 Mar, 17:56, Kai-Uwe Bux <jkherci...@gmx.netwrote:
SteveB wrote:
Peter
The questions and answers here are not just for you. They are also
meant as a source of information for others with similar problems.
see comment below.
does anyone know what the '*' represents at the beginning of the 300
?
Depends on your debugger if it's not part of the textual string value.
Read the post again Peter as Alf tells me very little.
On the contrary. Alf explains that you should check if the '*' is part
of the textual string. If it is not, you should consult your debugger
manual and find out why a '*' might be prepended. This last part is
off-topic in this group.
Perhaps you could tell me why the .data() function is no good with
wsprintf ? I can't find anything on google or these groups that
mentions any problems and the code works ok ?
Well - it is already there in the documentation of string::data and
the printf family. Read the documentation and you will have your
answer.
In my original post I pointed out that I was converting long numbers
to wide strings and that was the problem. The '*' is not part of the
original string. How could it be ?
In reply to your final point I can find nothing on Google pointing to
problems with wstring::data and wsprintf. If I had the time to readl
all the documentation I really wouldn't have to post any questions on
here at all !
Please indulge me and explain the problem you claim to be so obvious.

Sorry for jumping in: data() is not guaranteed to be 0-terminated. The
c_str() method is. The reason that you find the code working is that both
methods return the same pointer on your library implementation. In fact, I
don't know of any implementation that does otherwise.

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -
Thank you Kai - I'll check the code on Monday when I get into work and
change it to c_str()

Steve

Mar 17 '07 #17

P: n/a
On 17 Mar., 18:56, Kai-Uwe Bux <jkherci...@gmx.netwrote:
[snip]
>
Sorry for jumping in: data() is not guaranteed to be 0-terminated. The
c_str() method is. The reason that you find the code working is that both
methods return the same pointer on your library implementation. In fact, I
don't know of any implementation that does otherwise.
Neither do I, but what makes you believe that the buffer is 0-
terminated? If I wrote a std::string class, I guess that my
std::string::c_str would append the terminating 0 and return the
buffer. std::string::data would return the buffer without prepending
the 0.

/Peter

Mar 18 '07 #18

P: n/a
peter koch wrote:
On 17 Mar., 18:56, Kai-Uwe Bux <jkherci...@gmx.netwrote:
[snip]
>>
Sorry for jumping in: data() is not guaranteed to be 0-terminated. The
c_str() method is. The reason that you find the code working is that both
methods return the same pointer on your library implementation. In fact,
I don't know of any implementation that does otherwise.

Neither do I, but what makes you believe that the buffer is 0-
terminated?
The observation of the OP that his code appears to work. I would expect some
garbage in the output or a segfault otherwise.
If I wrote a std::string class, I guess that my
std::string::c_str would append the terminating 0 and return the
buffer. std::string::data would return the buffer without prepending
the 0.
Apparently, you did not write the string class that the OP used :-)
Best

Kai-Uwe Bux
Mar 18 '07 #19

P: n/a
Okay. I am a tad confused now. Is this correct ?

char *buffer = new char[(sizeof(long)*8+1)];
sprintf(buffer, "%d", 35L)
std::string strnum(buffer)
delete[] buffer;

or was the point above saying that i should add the '\0' to the buffer
manually as in:

char *buffer = new char[(sizeof(long)*8+1)];
sprintf(buffer, "%d\0", 35L)
std::string strnum(buffer)
delete[] buffer;

i was under the impression that the std::string constructor accepted a
char* as a paramter. is this wrong ?

thanks

Steve
Mar 19 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.