注册 登录
编程论坛 C++教室

整型如何转化为字符型?

阿冲 发布于 2009-10-06 15:24, 988 次点击
将1234转化为"1234",请问怎么转化??
4 回复
#2
kspliusa2009-10-06 16:07
回复 楼主 阿冲
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int num;
    char str[11], strtemp[11];
    while (cin>>num){
        int pn = 0;
        while (num!=0){
            str[pn++] = num %10 + '0';
            num /= 10;
            //cout<<str[pn]<<endl;
        }
        str[pn] = '\0';
        int i = 0;
        for (int k=pn-1; k>=0; k--){
            strtemp[i++] = str[k];
        }
        strtemp[pn] = '\0';
        cout <<strtemp<<endl;
    }
    return 0;
}
看看这个行不行!
#3
hyfl2009-10-06 18:01
#include <stdlib.h>
char* itoa(int value, char *str, int radix);
这个函数可以用
 
#4
haitao99992009-10-07 10:01
同意3楼。附一例代码。
#include <iostream>
#include <stdlib.h>
using namespace std;
 
 
int main(){
    int value;
    cin>>value;
    char buf[50];
    itoa(value,buf,10);
    cout<<endl<<buf;
    system("PAUSE");
}
#5
wghost2009-11-06 09:16
直接将他存放在字符数组中不、就得了吗?
1