十进制到字符串的转换

来源:互联网 发布:乐高玩具淘宝网 编辑:程序博客网 时间:2024/04/30 08:35

这种方法加以扩展,可以用于其他进制的数进行转换

  1. #include<iostream>
  2. using namespace std;
  3. void ItoC(char*A ,int m,int &counter)
  4. {
  5.     int temp1,temp2;
  6.     counter=1;
  7.     temp1 = m%10;
  8.     temp2 = m/10;
  9.     A[0]=temp1+48;
  10.     while(temp2 != 0)
  11.     {
  12.         m=temp2;
  13.         temp1 = m%10;
  14.         temp2 = m/10;
  15.         A[counter++]=temp1+48;
  16.     }
  17. }
  18. int main()
  19. {
  20.     char A[10];
  21.     int counter;
  22.     ItoC(A,34562,counter);
  23.     for(int i=0;i<counter;i++)
  24.         cout<<A[i]<<"/t";
  25.     cout<<endl;
  26.     while(counter-- >0)
  27.         cout<<A[counter]<<"/t";
  28.     cout<<endl;
  29.     return 0;
  30. }
原创粉丝点击