为什么前4个字符集为0? [英] Why the first 4 character set with 0?

查看:63
本文介绍了为什么前4个字符集为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写类似的转换函数,

string dbl2s(double dbl)

{

char chs [MAX_STRING_LENGTH] ;

memset(chs,0,sizeof(chs));

_snprintf_s(chs,_countof(chs),MAX_STRING_LENGTH,"%f",dbl);

返回字符串(chs);

}


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


我发现每次double值转换为char

数组时。前4个字符将设置为0,但从第5个字符开始将是/ b $ b是正确的值。所以最后字符串将被视为null

字符串。我用std :: string。


有人知道为什么吗?我非常感谢你的帮助。

解决方案

cutecutemouse写道:


我''我正在编写类似的转换函数,


string dbl2s(double dbl)

{

char chs [MAX_STRING_LENGTH];



我强烈建议初始化它:


char chs [MAX_STRING_LENGTH] = {};


然后你不需要以下声明。


memset(chs,0,sizeof(chs));

_snprintf_s(chs,_countof(chs),MAX_STRING_LENGTH,"%f",dbl);



该功能有什么作用?第二个和/ b
第三个参数有什么含义?你提供相同的价值,不是吗?


返回字符串(chs);

}


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


我发现每次double值转换为char

数组。前4个字符将设置为0,但从第5个字符开始将是/ b $ b是正确的值。所以最后字符串将被视为null

字符串。我用std :: string。


有人知道为什么吗?我真的很感谢你的帮助。



由于''_snprintf_s''不是标准函数,你应该

解释它应该做什么,或者发布到新闻组,主题是




V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问


我想将double值转换为字符串。我从一篇文章中读到了这个

函数。我只是想知道为什么这个问题发生了。$ / b
7月27日16:32,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:


cutecutemouse写道:


我正在编写类似的转换函数,


string dbl2s(double dbl)

{

char chs [MAX_STRING_LENGTH];



我强烈建议初始化它:


char chs [MAX_STRING_LENGTH] = {};


然后你不需要以下声明。


memset(chs,0,sizeof(chs));

_snprintf_s(chs,_countof(chs),MAX_STRING_LENGTH,"%f",dbl);



该功能有什么作用?第二个和/ b
第三个参数有什么含义?你提供的是相同的值,不是吗?


返回字符串(chs);

}


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


我发现每当double值转换为char

数组时。前4个字符将设置为0,但从第5个字符开始将是/ b $ b是正确的值。所以最后字符串将被视为null

字符串。我用std :: string。


有人知道为什么吗?我真的很感谢你的帮助。



由于''_snprintf_s''不是标准函数,你应该

解释它应该做什么,或者发布到新闻组,主题是




V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问




" cutecutemouse" < pl ****** @ hotmail.comwrote in message

news:f8 ************************ ********** @ m45g2000 hsb.googlegroups.com ...


7月27日16:32,Victor Bazarov< v .Abaza ... @ comAcast.netwrote:


> cutecutemouse写道:


我''我正在编写类似的转换函数,


string dbl2s(double dbl)

{

char chs [MAX_STRING_LENGTH];


我强烈建议初始化它:

char chs [MAX_STRING_LENGTH] = {};

然后你不要需要以下声明。


memset(chs,0,sizeof(chs));

_snprintf_s(chs,_countof(chs), MAX_STRING_LENGTH,%f,dbl);


该功能有什么作用?第二个和第三个论点的含义是什么?你提供的是相同的值,不是吗?


返回字符串(chs);

}


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


我发现每当double值转换为char

数组时。前4个字符将设置为0,但从第5个字符开始将是/ b $ b是正确的值。所以最后字符串将被视为null

字符串。我用std :: string。


有人知道为什么吗?我真的很感谢你的帮助。


由于''_snprintf_s''不是标准函数,你应该解释它应该做什么,或者发布到它所在的新闻组<关于主题。


>我想将double值转换为字符串。我从一篇文章中读到了这个

函数。我只是想知道为什么这个问题

发生了。



请不要发帖,重新安排消息。


我用Google搜索_snprintf_s并发现它''微软'b
sprintf的混蛋,是的,似乎第二和第三个参数在这种情况下应该是/ b $ b b是相同的。使用标准版本会给我们:


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


看起来不错。为了感到荣幸,我不知道问题是什么,

不确定这是微软的问题,还有别的东西等等。但是afaic它

无所谓因为_snprintf_s永远不会被使用,只需使用

stringstream。


std :: stringstream convert;

convert<< dbl;

std :: string value;

convert> value;


这类事情做得太多了,事实上,有一些模板用于

它。这是我使用的那个:


模板< typename T,typename FT StrmConvert(const F from)

{

std :: stringstream temp;

temp<< from;

T to = T();

temp> to;

返回;

}


模板< typename Fstd :: string StrmConvert(const F from)

{

返回StrmConvert< std :: string> (来自);

}


如果你使用提升,他们会有一个词汇演员,这是关于同样的事情。

无论如何,这很简单。


double Foo = 1234.56;

std ::: string Bar = StrmConvert(Foo);


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.

解决方案

cutecutemouse wrote:

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
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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...@comAcast.netwrote:

cutecutemouse wrote:

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
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



"cutecutemouse" <pl******@hotmail.comwrote in message
news:f8**********************************@m45g2000 hsb.googlegroups.com...

On 27 Jul., 16:32, Victor Bazarov <v.Abaza...@comAcast.netwrote:

>cutecutemouse wrote:

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


这篇关于为什么前4个字符集为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆