Why the first 4 character set with 0?

C

cutecutemouse

I'm writing a casting function like that,


string dbl2s(double dbl)
{
char chs[MAX_STRING_LENGTH];
memset(chs, 0, sizeof(chs));
_snprintf_s(chs, _countof(chs), MAX_STRING_LENGTH, "%f", dbl);
return string (chs);
}

------------------------------------------------------------------

I find that each time when the double value converts to the char
array. The first 4 char will be set to 0, but from the 5th one will
be the right value. So in the end the string will be treated as a null
string. I use std::string.

Does anybody know why? I really appreciate for your help.
 
C

cutecutemouse

I want to convert a double value to a string. And I read about this
function from an article. I just want to know why this problem
happened.
cutecutemouse said:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENGTH];

I would strongly recommend initialising it:

char chs[MAX_STRING_LENGTH] = {};

then you don't need the following statement.
memset(chs, 0, sizeof(chs));
_snprintf_s(chs, _countof(chs), MAX_STRING_LENGTH, "%f", dbl);

What does that function do? What are the meanings of the second and the
third argument? You're supplying the same value, no?
return string (chs);
}

I find that each time when the double value converts to the char
array. The first 4 char will be set to 0, but from the 5th one will
be the right value. So in the end the string will be treated as a null
string. I use std::string.
Does anybody know why? I really appreciate for your help.

Since '_snprintf_s' is not a standard function, you should either
explain what it's supposed to do, or post to the newsgroup where it is
on topic.

V
 
J

Jim Langston

cutecutemouse said:
cutecutemouse said:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENGTH];

I would strongly recommend initialising it:

char chs[MAX_STRING_LENGTH] = {};

then you don't need the following statement.
memset(chs, 0, sizeof(chs));
_snprintf_s(chs, _countof(chs), MAX_STRING_LENGTH, "%f", dbl);

What does that function do? What are the meanings of the second and the
third argument? You're supplying the same value, no?
return string (chs);
}

I find that each time when the double value converts to the char
array. The first 4 char will be set to 0, but from the 5th one will
be the right value. So in the end the string will be treated as a null
string. I use std::string.
Does anybody know why? I really appreciate for your help.

Since '_snprintf_s' is not a standard function, you should either
explain what it's supposed to do, or post to the newsgroup where it is
on topic.
I want to convert a double value to a string. And I read about this
function from an article. I just want to know why this problem
happened.

Please don't top-post, message rearranged.

I googled for _snprintf_s and found out it's Microsoft's bastardisation of
sprintf, and, yes, it seems the 2nd and 3rd parameters in this case should
be the same. Using the standard version would give us:

sprintf( chs, "%f", dbl );

which looks fine to me. To be hontest, I don't know what the problem is,
not sure if it's a Microsoft problem, something else, etc.. but afaic it
doesn't matter because _snprintf_s should never be used anyway, just use
stringstream.

std::stringstream convert;
convert << dbl;
std::string value;
convert >> value;

This kind of thing is done so much, in fact, that there are templates for
it. This is the one I use:

template<typename T, typename F > T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

template<typename F> std::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}

If you use boost they have a lexical cast that's about the same thing.
Anyway, it's rather simple.

double Foo = 1234.56;
std:::string Bar = StrmConvert( Foo );
 
C

cutecutemouse

Thank you for all the replies. I've just tried two ways. To use
stringstream, like the template what Jim said, and another way with
sprintf_s. But the problem is the same. The first 4 character are
always set to some strange chars. I'm really confused with it...
 
J

James Kanze

Only in broken newsreaders.

For your personal definition of broken.
All my messages are always posted in plain text, and not an
attachment:

The one Victor was replying to wasn't. Google groups may have a
number of problems, but it does allow displaying the actual
message sent, and your response was in a mime attachment.
Content-Type: text/plain; format=flowed; charset="US-ASCII"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

In the mime header. The basic policy for newsgroups which don't
specify otherwise is that mime is not allowed.
Please stop blaming me for known ten-year old bugs in
Microsoft's crapware. Microsoft's crapware can't wrap its
brain around digitally-signed messages, that all modern
newsreaders, such as Mozilla Thunderbird, have no problems
displaying, so it pukes and shows it as an attachment.

You might try to find out what you're really sending, first.
Go tell Microsoft to fix their broken newsreader, and
implement RFC 2015, the standard for digitally-signed messages
that's been published in 1996.

That standard involves Mime, and the standard for this (and most
groups) is that Mime isn't allowed.
 
A

acehreli

You are smoking crack.

I think you are on light beer.

[...]
Maybe that was the case last millenium

Hate to break the news to you, but this millenium has not started yet.
Following some fad marketeers has always been around, none of which
makes you modern nor wise.

[...]
Perhaps you would like to go back to the ancient does, where there were no
furriners around to bother you with their funny characters and words, but
the rest of the Internet has moved on, and MIME is here to stay.

Besides, let me look at /your/ messages:

# NNTP-Posting-Host: 77.206.241.95
# Mime-Version: 1.0
# Content-Type: text/plain; charset=ISO-8859-1
# Content-Transfer-Encoding: quoted-printable

Ohmegosh! Heavens to Betsy! You're posting MIME messages to Usenet!!!
And quoted-printable, to boot!!!!!! Don't you know that you shouldn't do
that?

Come to think of it, judging from that attitude, you might be abusing
caffeine.

I recommend you seek explanations before insulting people with your
immature comments. Thinking works too...

[...]
Not in this millenium. But, if you really believe so, go away and don't come
back until /you/ stop posting MIME formatted messages.

How about you don't come back until /you/ stop posting under
influence. And once again, your "this millenium" has not started yet.
application_pgp-signature_part
1KDownload

Is that how you sign your insults? So that we can be sure that it came
from "Sam" but no other person? May that's what "this millenium" is...

Ali
 
J

Jim Langston

cutecutemouse said:
Thank you for all the replies. I've just tried two ways. To use
stringstream, like the template what Jim said, and another way with
sprintf_s. But the problem is the same. The first 4 character are
always set to some strange chars. I'm really confused with it...

Okay, please give us a very small short program that displays the behavior
you are experiencing. It is possible you have something mistanken or
something, we can't tell because we don't know what you are doing. Please
show some compilable code that shows your problem.

Regards,

Jim Langston
 
J

James Kanze

My definition of broken includes violating section 5.1.7 of
RFC 2046. Does yours?

Where does RFC 2046 apply here? RFC 2046 applies to a specific
use of Mime, and Mime isn't considered acceptable in the big
eight, except when the charter of the newsgroup explicitely says
otherwise.
Yes, it was. Go read RFC 2045 and 2046, then get back to me.

I looked that the actual text. There was a Mime attachment.
Which is not acceptable in a newsgroup in the big 8.

Go learn a little about how news groups work, and get back to
us.
You are smoking crack. I just pulled up this thread in Google
Groups, and my messages are shown, plain and unadorned, just
fine.

So you don't know how to use Google newsgroups either.
Maybe that was the case last millenium, but there are now all
sorts of people across the world posting to Usenet, who speak
funny languages and write using funny characters;

Not in the big eight. Again, by default, in the big eight, Mime
is not considered acceptable, and English is the standard
language.
and using MIME headers to indicate the correct character set
and encoding that should be used to display their messages.

Not MIME. The encoding can, and should be specified in the
message header. You don't need or want MIME for this.
Perhaps you would like to go back to the ancient does, where
there were no furriners around to bother you with their funny
characters and words, but the rest of the Internet has moved
on, and MIME is here to stay.

Look at my .sig.
Besides, let me look at /your/ messages:
# NNTP-Posting-Host: 77.206.241.95
# Mime-Version: 1.0
# Content-Type: text/plain; charset=ISO-8859-1
# Content-Transfer-Encoding: quoted-printable
Ohmegosh! Heavens to Betsy! You're posting MIME messages to Usenet!!!
And quoted-printable, to boot!!!!!! Don't you know that you shouldn't do
that?

In the header. Not in a MIME attachment. Do you know the
difference between a message header and a MIME attachment? Do
you have even the slightest notion of how news works?
I know exactly what I'm sending.

Then why do you lie about it? All of your response was in a
MIME attachment.

Yes. Having managed newsgroup servers, and written automated
code the process email and news group messages, I also know the
difference between a newsgroup header and MIME. If you're
newsreader is attaching MIME by default, then it is broken.
Not in this millenium. But, if you really believe so, go away
and don't come back until /you/ stop posting MIME formatted
messages.

I don't post MIME. Because it is not allowed. You do.
Until such time you do, any bleating on your part,
regarding the same, would be rather hypocritical.

Until you learn how newsgroups work, you'd be better off
shutting up.
 
N

Nick Keighley

I'm writing a casting function like that,

string dbl2s(double dbl)
{
        char chs[MAX_STRING_LENGTH];
        memset(chs, 0, sizeof(chs));
        _snprintf_s(chs, _countof(chs), MAX_STRING_LENGTH, "%f", dbl);
        return string (chs);

}

------------------------------------------------------------------

I find that each time when the double value converts to the char
array. The first 4 char will be set to 0, but from the 5th  one will
be the right value. So in the end the string will be treated as a null
string. I use std::string.

Does anybody know why? I really appreciate for your help.

I don't have the problem with sprintf(). This is my code:

// cute.cpp

#include <cstdlib>
#include <cstdio>
#include <string>

using namespace std;

#define MAX_STRING_LENGTH 32

string dbl2s(double dbl)
{
char chs[MAX_STRING_LENGTH];

memset (chs, 0, sizeof(chs));
sprintf (chs, "%f", dbl);

return string (chs);
}

int main (void)
{
double d = 2.182;
string s = dbl2s (d);
const char* cstr = s.c_str();

for (int i = 0; i < s.size(); i++)
printf ("%02x ", cstr & 0xff);

printf ("\n%s\n", cstr);

return 0;
}

its output is:

C:\bin\Debug>cute.exe
32 2e 31 38 32 30 30 30
2.182000

there are no leading zeros
 
C

cutecutemouse

I really appreciate all your help. Now I found the way to solve the
problem by just switching the Release mode to Debug mode. I think that
could be some library conflict because I'm using AutoCAD ObjectARX
interface.
I'm just a newbie here and sometimes do not know well with the rules.
If I did something wrong, I beg your pardon this time.
 
D

Default User

cutecutemouse said:
I really appreciate all your help. Now I found the way to solve the
problem by just switching the Release mode to Debug mode. I think that
could be some library conflict because I'm using AutoCAD ObjectARX
interface.

That didn't "solve" anything. One common feature of debug modes is to
zero out automatic memory. Probably you have masked the error, rather
than solving it.




Brian
 
J

James Kanze

2046 is anything but specific. It coveres a wide swath of MIME
formatting.

Exactly. And since the standard policy in newsgroups in the big
eight is not to allow MIME, it doesn't apply here.
Then /you/ should stop posting using MIME formatting,
according to your own assertions.

I don't post in MIME.
Of course, that's bunk. MIME posting are always acceptable,
and have been that way for a long time.

Only since you've decided to change the rules. The consensus
is, and always has been, that MIME is not acceptable here.
Now look at the original whiner's complaint. That's not
exactly what he claimed.

Yes it is.

How is that relevant to the big eight. Brad Templeton created
ClariNet, and the article which you cite applies there, not in
the big eight. He is also moderator of rec.humor.funny, in the
big eight. Where the rules
(http://www.netfunny.com/rhf/submit.html) are:
- Plain text E-mail only, thanks. Not HTML, Not multipart
Note that the page is dated 1998. Welcome to the new millenium.

Note that the page doesn't apply to news groups in the big
eight, but to ClariNet.
Go check what millenium we're in, and if you prefer to go back
to the pre-MIME days, I'll help you find a TARDIS.

The millenium has nothing to do with it. Various newsgroups
establish the rules for that newsgroup, by consensus. And the
consensus in the big eight is that by default, MIME is not
allowed.
Well, I apparently can, since the thread came up just fine,
with mine's and your's messages, clearly shown for all to see.

Yep. And if you click on show original, you'll see that yours
consisted of two MIME attachments, one with your answer, and the
other with the PGP signature, where as mine had no MIME
attachments.
Really? soc.culture.venezuela, for example, does not exist?

I don't know. Any group in the big eight can make their own
rules, however. By default (and that's the case here, unless
you can show something to the contrary), MIME attachments are
not acceptable.
Wake up and smell the millenium.

Is that you're standard answer when you don't understand
something? The working language of comp.lang.c++ is English.
Regardless of the millenium.
Free clue. In MIME, there's only one kind of a "header". You
don't know what you're talking about.

I'd suggest you read some of the Internet RFC's before you make
a fool of yourself. (Oops. Too late for that now.)
When you've implemented, yourself, MIME parsing software, let
me know and we can discuss this intelligently.

I've worked on MIME parsing software, and I've worked on NNTP
software.
You apparently didn't, up to know, since you were stuck
precisely on solely the "MIME" part.
I'm fairly certain that I do.

Obviously not.
No, it wasn't. You keep getting confused, as the original
whiner, whether my message is composed entirely of an
attachment, or just a small digital signature part. Go look up
what "Content-Disposition: inline" means, and get back to me.

For your information (since you don't seem to be familar with
the Internet message format): the message header stops at the
first empty line. After than, your message contains a line:
--=_mimegpg-commodore.email-scan.com-21840-1217199795-0003
While that doesn't look like what I normally expect from MIME,
it is an attachment, and a good newsreader won't show it without
explicit intervention on the part of the reader.
It is not. Go look up what "Content-Disposition: inline"
means, and get back to me. Read that 1998 spec, if you must,
again.

"Content-Disposition: inline" means nothing here. The contents
of the message should be normal text, and should not be
interpreted by the newsreader.
Yes you do post MIME. Your every message carries a
MIME-Version: header. QED.

My messages do not contain any MIME. The header doesn't mean
anything (and is required by NNTP to be ignored) unless there is
actually a MIME attachment in the message.
Don't tell others to post MIME-formatted messages, until you,
yourself, stop doing that.
Says someone who still has his head in the sand, believing
that Big 8 is English only.

It would help the discussion if you'd read what the other person
wrote, instead of inventing things he never said. I said that
by default, the Bit 8 uses English. There are many groups in it
which are bi-lingual, or even exclusively some other language,
but that is stated explicitly in their charter. In the case of
comp.lang.c++, the language is English.
 
L

Lionel B

On Tue, 29 Jul 2008 21:53:25 -0500, Sam wrote:

[...]
You are operating under a rather major misconception. You obviously live
in perpetual fear of getting "plonked", given that admonishment of
yours. Well, see, not everyone shares your insecurities.

Attachment not shown: MIME type application/pgp-signature

Duh!
 
J

James Kanze

There is no such policy.

There always has been.

There's also a policy of lurking a month before posting, to see
what is acceptable and what isn't. Had you done that, you'd see
that no one else uses Mime attachments, because they aren't
considered acceptable. But of course, since you're the only one
who counts in your world, you can't be bothered by a thing like
that.
Yes, you do. Every one of your messages is MIME formatted.

Obviously, you don't know what MIME is (and haven't even read
the RFC which you signaled).

Anyway, I'm not going to waste my time arguing with you. Anyone
can read the relevant RFC's if they want.
No, it's not.

The original complaint was that he didn't automatically see your
response. Which is due to the fact that your response is in a
Mime attachment, and his newsreader doesn't automatically open
Mime attachments.

Note that your postings are the only ones which cause this
problem. So either you're wrong, or every one else here is
wrong.
It's a moderated newsgroup. Moderated newsgroups may set their
own rules.

All groups set their own rules. In the case of this group, the
group was founded before the current rules for group creation
went into effect, so it gets the default set of rules.

If you want a C++ group in which Mime attachments are allowed,
you're free to create one. There are standard procedured for
it.
There is no such concensus. I recall reading someone's survey,
that indicated that ~80% of Usenet messages are
MIME-formatted. That is anything but a consensus to the
opposite.

Yours are the only messages here which contain Mime attachments.
Which looks like a consensus to me.

Anyway, I give up. I can't argue with stupidity and anti-social
behavior. (It's not as if you actuallly contributed anything
positive to the group.)
 
C

Chris M. Thomasson

Lionel B said:
On Tue, 29 Jul 2008 21:53:25 -0500, Sam wrote:

[...]
You are operating under a rather major misconception. You obviously live
in perpetual fear of getting "plonked", given that admonishment of
yours. Well, see, not everyone shares your insecurities.

Attachment not shown: MIME type application/pgp-signature

Duh!

Lionel, AFAICT, whether he/she is right or wrong, SAM is acting like a
smart-ass punk. Well, I say that because he/she accused James Kanze of
"smoking crack"... What a nasty accusation especially since there was no
apparent sign of sarcasm in his/her overall tone.
 
C

Chris M. Thomasson

[...]

Thanks for the kind words.

My reply made me sound like a total smart-ass punk as well; sorry about
that.

;^(...

Anyway, I agree. Microsoft crapware does not like your messages and pukes
out an attachment... On the other hand, Pan displays them quite nicely.
Google displays then nicely. And several other do as well.
 
C

Chris M. Thomasson

Chris said:
[...]

Pan displays your MIME attachments inline.
In RFC 2183-context "attachment" and "inline" is mutually exclusive. A
given
MIME entity is either an attachment entity, or an inline entity, but not
both: [...]


What you obviously meant to say is that Pan shows the main content of my
messages inline, and not as an attachment, as Microsoft's crapware does.
Pan is, obviously, correct.

Indeed.
 
I

Ioannis Gyftos

All my messages are always posted in plain text, and not an attachment:

Content-Type: text/plain; format=flowed; charset="US-ASCII"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit


Strangely, I am getting this...

Content-Type: text/trolling; format=flowed_logic; charset="US-ASCII"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
 

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

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top