473,729 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why the first 4 character set with 0?

I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENG TH];
memset(chs, 0, sizeof(chs));
_snprintf_s(chs , _countof(chs), MAX_STRING_LENG TH, "%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.
Jul 27 '08 #1
26 1423
cutecutemouse wrote:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENG TH];
I would strongly recommend initialising it:

char chs[MAX_STRING_LENG TH] = {};

then you don't need the following statement.
memset(chs, 0, sizeof(chs));
_snprintf_s(chs , _countof(chs), MAX_STRING_LENG TH, "%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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '08 #2
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.
On 27 Jul., 16:32, Victor Bazarov <v.Abaza...@com Acast.netwrote:
cutecutemouse wrote:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENG TH];

I would strongly recommend initialising it:

char chs[MAX_STRING_LENG TH] = {};

then you don't need the following statement.
memset(chs, 0, sizeof(chs));
_snprintf_s(chs , _countof(chs), MAX_STRING_LENG TH, "%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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '08 #3

"cutecutemo use" <pl******@hotma il.comwrote in message
news:f8******** *************** ***********@m45 g2000hsb.google groups.com...
On 27 Jul., 16:32, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>cutecutemous e wrote:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENG TH];

I would strongly recommend initialising it:

char chs[MAX_STRING_LENG TH] = {};

then you don't need the following statement.
memset(chs, 0, sizeof(chs));
_snprintf_s(chs , _countof(chs), MAX_STRING_LENG TH, "%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::stringstre am 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<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}

template<typena me Fstd::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 );
Jul 27 '08 #4
cutecutemouse <pl******@hotma il.comwrote:
I'm writing a casting function like that,
string dbl2s(double dbl)
{
char chs[MAX_STRING_LENG TH];
memset(chs, 0, sizeof(chs));
_snprintf_s(chs , _countof(chs), MAX_STRING_LENG TH, "%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 know the answer, but as others have suggested, use a
stringstream.

Here's what I use:

template < typename T, typename U >
T lexical_cast( const U& u ) {
std::stringstre am ss;
T t;
if ( !( ss << u && ss >t ) ) throw std::bad_cast() ;
return t;
}
void foo( double dbl ) {
string str = lexical_cast<st ring>( dbl );
// now use str.
}
Jul 27 '08 #5
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...
Jul 27 '08 #6
cutecutemouse <pl******@hotma il.comwrote:
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...
Show us the stringstream code that exhibits the problem you are having.
Jul 27 '08 #7
Sam
cutecutemouse writes:
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...
Go ask Microsoft. You were told, repeatedly, that this is not a standard C++
function, but some Microsoft-specific rogue mutation. Only Microsoft can
tell you what it does or how it works.

This newsgroup is not for discussions of Microsoft-specific language
variations, but rather the standard C++ language.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAki Mv8QACgkQx9p3GY HlUOKaXQCePfIbp Wg982IXlcGy3KHG c45A
IYkAn1AY2duM+wQ MmrRnuOq5iA4QZR 6Z
=CnfR
-----END PGP SIGNATURE-----

Jul 27 '08 #8
"Sam" <sa*@email-scan.comwrote in message
news:co******** *************** *******@commodo re.email-scan.com...
[???]

Sam,

It's not the first time I saw a posting from you that does not
seem to have any content. It turns out you use some kind of
format that causes your reply to appear as an attachment (which
in a text-only newsgroup is prohibited, and many actually have
those disabled). Could you perhaps set your newsreader so that
no attachment is made out of your reply and instead plain text
is used, please? It would make the lives of many of us easier.
That is, if you care whether anybody actually reads what you
post...

Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '08 #9
Sam
Victor Bazarov writes:
Sam,

It's not the first time I saw a posting from you that does not
seem to have any content. It turns out you use some kind of
format that causes your reply to appear as an attachment (which
Only in broken newsreaders.
in a text-only newsgroup is prohibited, and many actually have
those disabled). Could you perhaps set your newsreader so that
no attachment is made out of your reply
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
and instead plain text
is used, please?
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.
It would make the lives of many of us easier.
That is, if you care whether anybody actually reads what you
post...
Go tell Microsoft to fix their broken newsreader, and implement RFC 2015,
the standard for digitally-signed messages that's been published in 1996.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAki M2K4ACgkQx9p3GY HlUOIF0ACeMvd3o iGfr8Z6R73QB0V9 iG3B
LIAAnA6/BQNuMxTG2fgljAu ckgj2khPp
=oNGh
-----END PGP SIGNATURE-----

Jul 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
11645
by: FrancisC | last post by:
How can I open a file, skip first 2 lines and get the 50th character? EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00 ARC 2 1 1 0 0 0 0 7 i.e., I want to get the "7" in the third line, how can I do that? thx!!
2
1983
by: Gary | last post by:
Morning all, I have a form field called: Bsk01 How do I onBlur prompt the user to enter a ZERO as character one, if one is not already entered. At the same time, I would like to ensure at least 5t characters have been entered. This validation should also only trigger if anything is entered. Basically, if the user chooses to enter nothing then the check should not be carried out and the user should be able to continue.
8
7638
by: Mr. B | last post by:
In VB6, I had some code which 'forced' the first character of a string entered to be Capital. For example, if a person was entering their name (john doe)... the code would 'force' --- John Doe. Here is what I believe is the VB6 code: Private Sub txbModUser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txbModUser.TextChanged KeyAscii = AutoType(Screen.ActiveControl, KeyAscii) End Sub
4
1447
by: Theron NightStar | last post by:
I am trying to teach myself c++. This is the first program I have ever written that might have an practical use to me. I rather proud of it since like implied - I have no real knowledge of c++. Anyways, here is my problem - This prog does what it is supposed to (a very very crude encryption/decryption of text files) but for some reason it always duplicates the last character 2 extra times. If I were to run the word this
15
2287
by: Beeeeeves | last post by:
Is there a quick way to find the index of the first character different in two strings? For instance, if I had the strings "abcdefghijkl" and "abcdefxyz" I would want the return value to be 6. Either in C# or C++. (I obviously know how to do it by looping through the
2
2071
by: D.Frangiskatos | last post by:
Hi, I have been working for a few months in project that deals raw sockets. However recently, and while trying to examine the contents of the buffer used in recvfrom i was a bit confused. The buffer was allocated using malloc as it can be seen next: do { ..............
43
2804
by: Roger L. Cauvin | last post by:
Say I have some string that begins with an arbitrary sequence of characters and then alternates repeating the letters 'a' and 'b' any number of times, e.g. "xyz123aaabbaabbbbababbbbaaabb" I'm looking for a regular expression that matches the first, and only the first, sequence of the letter 'a', and only if the length of the sequence is exactly 3.
36
3199
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with the needed data. Any ideas? Here's the code in question, see any reason why a command would not trigger the 'kbhit' the first time a serial command is sent?: Thanks! Chuck **************************************************** while(1) //...
26
4530
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
5
2371
by: sniipe | last post by:
Hi, I have a problem with unicode string in Pylons templates(Mako). I will print first char from my string encoded in UTF-8 and urllib.quote(), for example string '£ukasz': ${urllib.unquote(c.user.firstName).encode('latin-1')} and I received this information:
0
8917
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
8761
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
9426
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
9281
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
9142
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
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4525
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2680
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.