最近发现做程序时经常会遇到整型与字符型的相互转换的问题,所以我今晚就狠下心花点时间整理了下,以下是我的整理的笔记,都是关键代码,具体大家再实现吧,如有问题与我联系:
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;
}
整型与字符型的相互转换
最新推荐文章于 2023-01-11 15:59:42 发布