473,387 Members | 1,535 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.

wide string problem ...

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
19 3619
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
* 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
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
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
* 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
* 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
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
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
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
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
* 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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jonathan Mcdougall | last post by:
I started using boost's filesystem library a couple of days ago. In its FAQ, it states "Wide-character names would provide an illusion of portability where portability does not in fact exist....
15
by: Steve | last post by:
Hi, I've been charged with investigating the possibilities of internationalizing our C++ libraries. std::strings are used all over the place, and unfortunately a mixture of...
11
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but...
1
by: ajay.sonawane | last post by:
How can I find the wheather the occurance substring is main string in wide char provided that the compasion should be case insensitive.
1
by: jjf | last post by:
Do Standard C's wide characters and wide strings require absolutely that each character be stored in a single wchar_t, or can characters be "multi-wchar_t" in the same way that they can be...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
4
by: uday.sen | last post by:
Hi, I need to convert a string from UTF8 to wide character (wchar_t *). I perform the same in windows using: MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen); However, in linux...
4
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
9
by: toton | last post by:
Hi, I have my program using wstring everywhere instead of string. Similarly I need to process some file, which contains unicode or ascii character. I need to stream them. Thus I use wifstream etc....
1
by: iwongu | last post by:
Hi, I have a question about std::wcout and its underlying C functions. According to C99, a stream have an orientation type that is one of byte-oriented or wide-one, and it is determined by the...
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: 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:
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
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...

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.