wide string problem ...

S

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;

}


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
 
M

mimi

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);
 
A

Alf P. Steinbach

* 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.
 
S

SteveB

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
 
S

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.
 
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.
 
S

SteveB

* Alf P. Steinbach:



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.
 
P

peter koch

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
 
S

SteveB

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.
 
P

peter koch

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
 
A

Alf P. Steinbach

* SteveB:
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?
 
S

SteveB

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
 
P

peter koch

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.


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
 
S

SteveB

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.
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.


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.
 
K

Kai-Uwe Bux

SteveB said:
Peter


see comment below.


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
 
S

SteveB

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
 
P

peter koch

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
 
K

Kai-Uwe Bux

peter said:
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
 
S

SteveB

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top