字符串与整型转化

1.整型-字符串

  • 将一个多位的整数以字符串的形式存储,即将整数的每一位取出以ASCII码格式存储;
  • Num%10取出个位,Num/10取出个位,逆序的取出每一位数;
  • num+'0' 整数-ASC字符转化;
  • 用栈存储字符;
  • 可以直接用itoa转换;
#include <iostream>
#include  <stack>
int main(int argc, const char * argv[])
{

    // insert code here...
    using namespace std;
    int num=123456,i=0;
    
    char str[7];
    stack<char> sc;//栈容器无迭代器

    for (int i=0; i<6; i++)
    {
        char temp=num%10+'0';//取出个位数并转换为数值对应的ASC字符
        sc.push(temp);
        num=num/10;
    }

    while (!sc.empty())
    {
        str[i]=sc.top();//取栈顶元素和出站操作是独立分开的
        sc.pop();
        i++;
        
    }
    cout<<str;
    return 0;
}
原文地址:https://www.cnblogs.com/helo-blog/p/3302857.html