整数到字符串转换 [英] Integer to string conversion

查看:72
本文介绍了整数到字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友:


我知道有些人不喜欢回答C ++问题,但是我相信这真的是关于底层问题的C代码。无论如何,我已经发布

以及其他有人建议的小组(comp.lang.c ++)只是在

的情况下。


我认为问题在于,在我的C ++课程中,我们被教导使用cin,

cout等,但现在我需要使用原始的C操作printf等。

我不知道它们是如何工作的。


我正在尝试将整数转换为字符串。这就是我正在尝试的,

但是当我尝试输出从函数返回的字符串时,我只是

得到垃圾。我应该如何使用sprintf?


谢谢。


char * App :: IntToString(int i)//不是真的依靠课程

{

char buf [2];

char * out = buf;

sprintf( out,%d,i);

退出;

}

解决方案

< blockquote> pradeep< no **** @ nospam.comwrites:


我知道有些人不喜欢回答C ++问题,但我是/>
相信这是关于底层C代码的。无论如何,我已经将

发布到有人建议的其他组(comp.lang.c ++)

以防万一。


我认为问题在于,在我的C ++课程中,我们被教导使用

cin,cout等,但现在我需要使用原始的C操作printf

等我并不完全明白它们是如何工作的。


我正在尝试将整数转换为字符串。这就是我想要的b $ b尝试,但是当我尝试输出从

函数返回的字符串时,我只是得到了垃圾。我该如何使用sprintf?



[...]


>

char * App :: IntToString(int i)//并不真正依赖于课程

{

char buf [2];

char * out = buf;

sprintf(out,"%d",i);

退出;

}



您可以(并且应该)避免任何可能的争议(a)

删除::从你的函数名称,(b)删除任何提及的元数据和b ++的b $ b,以及(c)不交叉发布到comp.lang.c ++。你那么
会在C新闻组中询问一个合适的C问题

(我已经重定向了后续跟踪)。


就C而言,常见问题解答位于< http://www.c-faq.com/>和

你问过问题7.5a。阅读那里的答案,并按照

引用其他问题。如果那不能告诉你你需要什么

要知道,只需要跟进comp.lang.c。


或者,如果你只是由于某些原因,你想在C ++代码中使用sprintf,你可以将
发布到comp.lang.c ++。他们无疑会问你

为什么你不只是使用通常的C ++习语 - 这是一个非常好的

问题,你可以考虑回答一个问题在

comp.lang.c ++的后续版本中。


-

Keith Thompson(The_Other_Keith)< ks * **@mib.org>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


感谢所有答案。


我认为Jim可能有最好的解决方案但是我们还没有覆盖

模板但它们看起来很混乱,代码在前面模糊不清

我的眼睛!


我想要的是一个更简单的版本。这是我的想法,我认为我认为这与Jim尝试做的非常相似但更容易:制作一个管道

然后cout>整数到管道,但然后cin<<它返回

字符串。这有用吗?


我的电脑上有一个管道命令,但在

教科书中没有提到。我不明白这个论点,它是一个2 int的数组。我试图传递NULL但是它没有工作。


谢谢。

Jim Langston写道:


pradeep写道:


>>你好朋友:

我认识一些人这里不想回答C ++问题,但我相信这是关于底层C代码的。无论如何,我已经将其他人建议
(comp.lang.c ++)发布给以防万一。

我认为问题是在我的C ++课程中,我们被教导使用cin,cout等,但现在我需要使用原始的C操作
printf等,我不知道它们是如何工作的。

我正在尝试将整数转换为字符串。这就是我在尝试的东西,但是当我尝试输出从
函数返回的字符串时,我只是得到了垃圾。我应该如何使用sprintf?

谢谢。

char * App :: IntToString(int i)//并不真正依赖于课程
{
char buf [2];




到目前为止还不够大,一旦IntToString结束就消失了。


> char * out = buf;




没理由必须这样做。


> sprintf(out,"%d",i);




sprintf(buf,"%d",i);

也可以,虽然有其他的问题。


>退出;






函数返回时,你返回一个指向超出范围的缓冲区的指针。


>>}




那么,如何解决这个问题?最简单的方法是使缓冲区保持静态,这样当函数返回并且足够大时,它就不会消失。 char buf [2];

只大到足以容纳0。通过9。但是,整数可以保持相当大的
值。我认为10或11位数。


char * App :: IntToString(int i)//并不真正依赖类

{

static char buf [12];

sprintf(buf,"%d",i);

返回buf;

}


Shoudl修复了即时错误,但没有解决任何设计问题。这样的事实如果你连续两次调用这个事实,两者都返回char *'s将

指向相同的值。 IE


char * Value1;

char * Value2;


Value1 = IntToString(10);

Value2 = IntToString(11);


std :: cout<<值1<< " " << Value2;


将输出11 11,因为IntToString返回的指针是相同的,

并且静态缓冲区已更改。


最好返回一个std :: string。您既可以按原样进行转换,也可以转换为std :: string,或者使用std :: stringstream。 (未经测试

代码)。


std :: string App :: IntToString(int i)

{

std :: stringstream转换;

转换<< i;

std :: string答案;

转换>答案;


返回Convert.string();

}


就个人而言,我使用了一个模板。


模板< 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> (来自);

}


所以我可以这样做:


std :: string Value = StrmConvert( 10);


甚至:


int Value = StrmConvert< int>(" 10");


请注意,我的StrmConvert没有任何错误检查,任何错误都会返回默认的构造值。 I.E.如果您尝试转换

" xyz"结果将为0.


Jim Langston写道:


pradeep写道:


>感谢所有答案。

我认为Jim可能有最好的解决方案,但我们还没有涵盖还有模板,它们看起来很混乱,代码在我眼前只是模糊不清!

我想要的是一个更简单的版本。这是我的一个想法
我认为它与Jim尝试做的非常相似但更容易:
制作一个管道,然后cout>整数到管道,但然后cin<<
它回来了一个字符串。那会有用吗?

我的电脑上有一个管道命令,但在
教科书中没有提到。我不明白这个论点,它是一个2 int的数组。我尝试传递NULL,但它没有用。



是的。这就是我给出的字符串流示例。


std :: string App :: IntToString(int i)

{

std :: stringstream转换;

转换<< i;

std :: string答案;

转换>答案;


返回Convert.string();

}



无法编译。这将:


#include< sstream>

#include< string>


std :: string IntToString(int i)

{

std :: ostringstream转换;

转换<< i;

返回Convert.str();

}


std :: stringstream在这种情况下管道。而不是cin而不是cin和

" cout"您使用实例化的字符串流的名称。请确保你

#include< sstream>

#include< string>



你知道这是comp.lang.c吗?

f''ups设置为clc ++


-

Thomas


Hello friends:

I know some people here don''t like to answer C++ questions, but I
believe this is really about the underlying C code. Anyway I have posted
as well to the other group someone suggested ("comp.lang.c++") just in
case.

I think the problem is that in my C++ class, we were taught to use cin,
cout etc. but now I need to use the primitive C operations printf etc.
and I don''t understand exactly how they work.

I''m trying to convert an integer into a string. Here''s what I''m trying,
but when I try to output the string returned from the function, I just
get garbage. How should I be using sprintf?

Thanks.

char *App::IntToString(int i) //doesn''t really rely on classes
{
char buf[2];
char *out = buf;
sprintf(out, "%d", i);
return out;
}

解决方案

pradeep <no****@nospam.comwrites:

I know some people here don''t like to answer C++ questions, but I
believe this is really about the underlying C code. Anyway I have
posted as well to the other group someone suggested ("comp.lang.c++")
just in case.

I think the problem is that in my C++ class, we were taught to use
cin, cout etc. but now I need to use the primitive C operations printf
etc. and I don''t understand exactly how they work.

I''m trying to convert an integer into a string. Here''s what I''m
trying, but when I try to output the string returned from the
function, I just get garbage. How should I be using sprintf?

[...]

>
char *App::IntToString(int i) //doesn''t really rely on classes
{
char buf[2];
char *out = buf;
sprintf(out, "%d", i);
return out;
}

You could (and should) have avoided any possible controversy by (a)
dropping the "::" from your function''s name, (b) dropping any mention
of C++ and classes, and (c) not cross-posting to comp.lang.c++. You
would then have been asking an appropriate C question in a C newsgroup
(to which I''ve redirected followups).

As far as C is concerned, the FAQ is at <http://www.c-faq.com/>, and
you''ve asked question 7.5a. Read the answer there, and follow the
references to other questions. If that doesn''t tell you what you need
to know, post a followup to comp.lang.c only.

Or, if you just wanted to use sprintf in C++ code for some reason, you
could have just posted to comp.lang.c++. They''d undoubtedly ask you
why you don''t just use the usual C++ idioms -- which is a very good
question, and one you might consider answering in a followup to
comp.lang.c++ only.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Thanks for all the answers.

I think Jim probably has the best solution but we haven''t covered
templates yet and they seem very confusing, the code just blurs in front
of my eyes!

What I''d like is a simpler version of this. Here''s an idea I had which I
think is very similar to what Jim tries to do but easier: make a pipe
and then cout>the integer to the pipe, but then cin<< it back as a
string. Would that work?

There is a pipe command on my computer but it isn''t mentioned in the
text book. I don''t understand the argument, it''s an array of 2 int. I
tried passing NULL but it didn''t work.

Thanks.
Jim Langston wrote:

pradeep wrote:

>>Hello friends:

I know some people here don''t like to answer C++ questions, but I
believe this is really about the underlying C code. Anyway I have
posted as well to the other group someone suggested
("comp.lang.c++") just in case.

I think the problem is that in my C++ class, we were taught to use
cin, cout etc. but now I need to use the primitive C operations
printf etc. and I don''t understand exactly how they work.

I''m trying to convert an integer into a string. Here''s what I''m
trying, but when I try to output the string returned from the
function, I just get garbage. How should I be using sprintf?

Thanks.

char *App::IntToString(int i) //doesn''t really rely on classes
{
char buf[2];



Not big enough by far and disappears once IntToString finishes.

> char *out = buf;



No reason to have to do this.

> sprintf(out, "%d", i);



sprintf( buf, "%d", i );
would also work, although there are hte other problems.

> return out;



you are returning a pointer to a buffer that goes out of scope when the
function returns.

>>}



So, how to fix this? Simplest is to make the buffer static so it doesn''t
disappear when the function returns and make it big enough. char buf[2];
is only large enough to hold "0" through "9". An integer can hold quite a
larger value, however. 10 or 11 digits I think.

char *App::IntToString(int i) //doesn''t really rely on classes
{
static char buf[12];
sprintf(buf, "%d", i);
return buf;
}

Shoudl fix the immediate errors, but does not fix any design issues. Such
as the fact if you called this twice in a row, both returned char*''s would
point to the same value. I.E.

char* Value1;
char* Value2;

Value1 = IntToString( 10 );
Value2 = IntToString( 11 );

std::cout << Value1 << " " << Value2;

would output 11 11 since the pointers returned by IntToString are identical,
and the static buffer was changed.

It is better to return a std::string. You can do the conversion either as is
and convert to a std::string, or use use std::stringstream. (Untested
code).

std::string App::IntToString( int i )
{
std::stringstream Convert;
Convert << i;
std::string Answer;
Convert >Answer;

return Convert.string();
}

Personally, I use a template for this.

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

So I can do:

std::string Value = StrmConvert( 10 );

or even:

int Value = StrmConvert<int>( "10" );

Note that my StrmConvert does not have any error checking, any errors would
simply return a default constructed value. I.E. If you tried to convert
"xyz" to an int the result would be 0.


Jim Langston wrote:

pradeep wrote:

>Thanks for all the answers.

I think Jim probably has the best solution but we haven''t covered
templates yet and they seem very confusing, the code just blurs in
front of my eyes!

What I''d like is a simpler version of this. Here''s an idea I had
which I think is very similar to what Jim tries to do but easier:
make a pipe and then cout>the integer to the pipe, but then cin<<
it back as a string. Would that work?

There is a pipe command on my computer but it isn''t mentioned in the
text book. I don''t understand the argument, it''s an array of 2 int. I
tried passing NULL but it didn''t work.


Yes. That is what the stringstream example I also gave did.

std::string App::IntToString( int i )
{
std::stringstream Convert;
Convert << i;
std::string Answer;
Convert >Answer;

return Convert.string();
}

Doesn''t compile. This will:

#include <sstream>
#include <string>

std::string IntToString( int i )
{
std::ostringstream Convert;
Convert << i;
return Convert.str();
}

std::stringstream is in this case the "pipe". Just instead of "cin" and
"cout" you use the name of the instantized stringstream. Just make sure you
#include <sstream>
#include <string>

You are aware that this is the comp.lang.c?
f''ups set to c.l.c++

--
Thomas


这篇关于整数到字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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