十进制数用int表示,其他进制用string表示
#include <iostream>
#include <algorithm>
using namespace std;
string cov10to_any(int n,int radix){ //radix指几进制
string res;
while(n>0){
res+=(char)('0'+n%radix);
n/=radix;
}
reverse(res.begin(),res.end());
return res;
}
int covanyto_10(string n,int radix){
int res=0;
int tmp=1;
for(int i=n.length()-1;i>=0;i--){
res+=(n[i]-'0')*tmp;
tmp*=radix;
}
return res;
}
int main(){
cout<<cov10to_any(13,7)<<endl;
cout<<covanyto_10("16",7);
return 0;
}
原文链接: https://www.cnblogs.com/szh-blog/p/13476826.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/201386
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!