将1944转换为'1','9','4','4' [英] converting 1944 to '1','9','4','4'

查看:56
本文介绍了将1944转换为'1','9','4','4'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将1944转换为1,9,4,4


如何转换1944等数字到字符数组?


谢谢!

解决方案

x写道:

将1944转换为''1',''9',''4'','4''
如何将1944之类的数字转换为字符数组?




将它转换为字符串,然后将字符串转换为字符数组,如果

字符串对你不够好。


参见常见问题:

[38.1]如何将值(例如数字)转换为std :: string?
http://www.parashift.com/c++- faq-lit .... html#faq-38.1


2004年4月13日08:08:24 -0700, ao **** @ hotmail.com (x)写道:

将1944转换为''1'',''9'',''4'',''4''

我怎么能将诸如1944之类的数字转换为字符数组?

谢谢!




你可以去复古使用sprintf(这将是我的选择,实际上,是一个复古的家伙),或者如果您愿意,可以使用C ++流:


#include < iostream>

#include< cstdio>

#include< sstream>

使用命名空间std;


int main()

{

int y = 1944;


char buffer [10];

sprintf(buffer,"%d",y);

cout<< 使用sprintf: <<缓冲区<< endl;


ostringstream os;

os<< y;

strcpy(buffer,os.str()。c_str());

cout<< using ostringstream: <<缓冲区<<结束;


返回0;

}


-leor


-

Leor Zolman --- BD软件--- www。 bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误信息Decryptor at:
www.bdsoft.com/tools/stlfilt.html


Leor Zolman< le ** @ bdsoft.com>这样说:

int y = 1944;
char buffer [10];
sprintf(buffer,"%d",y);



在这种情况下没问题,但在我的系统上,INT_MAX是2147483647,

太大了,不适合10个字符的缓冲区:)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


converting 1944 to ''1'',''9'',''4'',''4''

how can I convert a number such as 1944 to a character array?

thanks!

解决方案

x wrote:

converting 1944 to ''1'',''9'',''4'',''4''

how can I convert a number such as 1944 to a character array?



Corvert it to string, then convert string into character array, if
string is not good enough for you.

See faq:
[38.1] How do I convert a value (a number, for example) to a std::string?
http://www.parashift.com/c++-faq-lit....html#faq-38.1


On 13 Apr 2004 08:08:24 -0700, ao****@hotmail.com (x) wrote:

converting 1944 to ''1'',''9'',''4'',''4''

how can I convert a number such as 1944 to a character array?

thanks!



You can go "retro" with sprintf (that would be my choice, actually, being a
retro kind of guy), or use C++ streams if you prefer it:

#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;

int main()
{
int y = 1944;

char buffer[10];
sprintf(buffer, "%d", y);
cout << "using sprintf: " << buffer << endl;

ostringstream os;
os << y;
strcpy(buffer, os.str().c_str());
cout << "using ostringstream: " << buffer << endl;

return 0;
}

-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


Leor Zolman <le**@bdsoft.com> spoke thus:

int y = 1944;
char buffer[10];
sprintf(buffer, "%d", y);



No problem in this case, but on my system, INT_MAX is 2147483647,
which is too big to fit in a 10-character buffer :)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于将1944转换为'1','9','4','4'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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