c++如何把整数1234转化成字串“1234”

c++如何把整数1234转化成字串“1234”老师布置的题让自己找答案,高手教下

告诉你一个简单的,常用的方法,就是itoa函数。
函数的原型为 char *itoa(int value, char *string, int radix);
其中 第一个是整型值,就是你要准换的数字;第二个是字符串指针,存储转换后的字符串;第三个是进制标志。
#include <iostream>
#include <stdlib.h>

using namespace std;

void main()
{
int i=1234;
char num[5]={'\0'};

itoa(i,num,10);
cout<<num<<endl;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-07
常用的方法,就是itoa函数。
函数的原型为 char *itoa(int value, char *string, int radix);
其中 第一个是整型值,就是你要准换的数字;第二个是字符串指针,存储转换后的字符串;第三个是进制标志。
#include <iostream>
#include <stdlib.h>

using namespace std;

void main()
{
int i=1234;
char num[5]={'\0'};

itoa(i,num,10);
cout<<num<<endl;
}
第2个回答  2009-03-11
下面是MSDN中的代码(也有例子):Converts an integer to a string. More secure versions of these functions are available; see _itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s.

char *_itoa(
int value,
char *str,
int radix
);
char *_i64toa(
__int64 value,
char *str,
int radix
);
char * _ui64toa(
unsigned _int64 value,
char *str,
int radix
);
wchar_t * _itow(
int value,
wchar_t *str,
int radix
);
wchar_t * _i64tow(
__int64 value,
wchar_t *str,
int radix
);
wchar_t * _ui64tow(
unsigned __int64 value,
wchar_t *str,
int radix
);
template <size_t size>
char *_itoa(
int value,
char (&str)[size],
int radix
); // C++ only
template <size_t size>
char *_i64toa(
__int64 value,
char (&str)[size],
int radix
); // C++ only
template <size_t size>
char * _ui64toa(
unsigned _int64 value,
char (&str)[size],
int radix
); // C++ only
template <size_t size>
wchar_t * _itow(
int value,
wchar_t (&str)[size],
int radix
); // C++ only
template <size_t size>
wchar_t * _i64tow(
__int64 value,
wchar_t (&str)[size],
int radix
); // C++ only
template <size_t size>
wchar_t * _ui64tow(
unsigned __int64 value,
wchar_t (&str)[size],
int radix
); // C++ only

Parameters
value
Number to be converted.

str
String result.

radix
Base of value; which must be in the range 2–36.

Return Value
Each of these functions returns a pointer to str. There is no error return.

Remarks
The _itoa, _i64toa, and _ui64toa functions convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 characters for _itoa and 65 for _i64toa and _ui64toa) in str. If radix equals 10 and value is negative, the first character of the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa, respectively.

Security Note:
To prevent buffer overruns, ensure that the str buffer is large enough to hold the converted digits plus the trailing null-character and a sign character.

In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. For more information, see Secure Template Overloads.

Tchar.h routine
_UNICODE and _MBCS not defined
_MBCS defined
_UNICODE defined

_itot
_itoa
_itoa
_itow

_i64tot
_i64toa
_i64toa
_i64tow

_ui64tot
_ui64toa
_ui64toa
_ui64tow

Requirements
Routine
Required header

_itoa
<stdlib.h>

_i64toa
<stdlib.h>

_ui64toa
<stdlib.h>

_itow
<stdlib.h>

_i64tow
<stdlib.h>

_ui64tow
<stdlib.h>

For more compatibility information, see Compatibility in the Introduction.

Example
Copy Code
// crt_itoa.c
// compile with: /W3
// This program makes use of the _itoa functions
// in various examples.

#include <string.h>
#include <stdlib.h>

int main( void )
{
char buffer[65];
int r;
for( r=10; r>=2; --r )
{
_itoa( -1, buffer, r ); // C4996
// Note: _itoa is deprecated; consider using _itoa_s instead
printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
}
printf( "\n" );
for( r=10; r>=2; --r )
{
_i64toa( -1L, buffer, r ); // C4996
// Note: _i64toa is deprecated; consider using _i64toa_s
printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
}
printf( "\n" );
for( r=10; r>=2; --r )
{
_ui64toa( 0xffffffffffffffffL, buffer, r ); // C4996
// Note: _ui64toa is deprecated; consider using _ui64toa
printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
}
}

Copy Code
base 10: -1 (2 chars)
base 9: 12068657453 (11 chars)
base 8: 37777777777 (11 chars)
base 7: 211301422353 (12 chars)
base 6: 1550104015503 (13 chars)
base 5: 32244002423140 (14 chars)
base 4: 3333333333333333 (16 chars)
base 3: 102002022201221111210 (21 chars)
base 2: 11111111111111111111111111111111 (32 chars)

base 10: -1 (2 chars)
base 9: 145808576354216723756 (21 chars)
base 8: 1777777777777777777777 (22 chars)
base 7: 45012021522523134134601 (23 chars)
base 6: 3520522010102100444244423 (25 chars)
base 5: 2214220303114400424121122430 (28 chars)
base 4: 33333333333333333333333333333333 (32 chars)
base 3: 11112220022122120101211020120210210211220 (41 chars)
base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)

base 10: 18446744073709551615 (20 chars)
base 9: 145808576354216723756 (21 chars)
base 8: 1777777777777777777777 (22 chars)
base 7: 45012021522523134134601 (23 chars)
base 6: 3520522010102100444244423 (25 chars)
base 5: 2214220303114400424121122430 (28 chars)
base 4: 33333333333333333333333333333333 (32 chars)
base 3: 11112220022122120101211020120210210211220 (41 chars)
base 2: 1111111111111111111111111111111111111111111111111111111111111111 (64 chars)

.NET Framework Equivalent
System::Convert::ToString

慢慢看吧
第3个回答  2009-03-11
方法很多啊。
例如:
CSTRING STMP;
int tmp = 1234;
STMP.FORMAT("%d",tmp );
这样,就把INT型的1234放到CSTRING里了。CSTRING和CHAR就都可以直接转换了。
也可放CHAR里,加油!
第4个回答  2009-03-11
思路是这样的,采用一个除10取余的方法来实现:
第一次:1234/10=123,1234%10=4;
第二次:1234/10=12,123%10=3;
第三次:12/10=1,12%10=2;
第四次:1/10=0用于判断循环结束,1%10=1
也就是每次相除的结果作为下一次循环的被除数,每次相除的余数作为字符串……
相似回答