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

来源:百度知道 编辑:UC知道 时间:2024/06/08 07:14:52
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;
}

#include <stdio.h>

int main()
{
char buf[100]; /* 大小自己定 */
snprintf(buf, 100, "%d", 1234);
printf("%s\n", buf);
return 0;
}

/*
snprintf函数的用法在这里说一下
函数原型
int snprintf(char *str, size_t size, const char *format, ...);
返回值: 若成功返回填入数组str中的字符数,若出错返回负数
作用:将数据使用format字符串表示的格式填入str所指向的缓冲区,缓冲区大小由size指定,合理的使用size参数能有效防止缓冲区溢出
*/

#include<strstream.h>
int main()
{int a=1234;
char c;
istrstream strin(a,sizeof(a));
strin>>c[i];
cout<<"char c:&