整型与字符型的相互转换

最近发现做程序时经常会遇到整型与字符型的相互转换的问题,所以我今晚就狠下心花点时间整理了下,以下是我的整理的笔记,都是关键代码,具体大家再实现吧,如有问题与我联系:
  Mail : rainday163@163.com
  整型到字符型的转换

1. char str[20];
int a=10;
sprintf(str,"%d",a);

2. int n=123;
string str;
stringstream stream;
stream<<n;
stream>>str;

3. int n=9;
char ch;
ch=n+’0’;

4. int fun(int a,string &str)
{
   while(a)
   {
    str+=string(a%10+’0’);
    a/=10;     
   }
   reverse(str.begin(),str.end());
   return 0;
}

字符型到整型的转换

1. char str[]="123";
int n;
sscanf(str,"%d",&n);

2. string str="123";
int n;
stringstream stream;
stream<<str;
stream>>n;

3. char ch = ’3’;
int n;
n=ch-’0’;

4. int fun(const string &str,int &n)
{
   n=0;
   for(int i=0;i<str.size();i++)
   {
    n*=10;
    n+=(str[i]+’0’);
   }  
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值