C++16进制转换

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan

#include<iostream.h>

int zhuanhuan(int x)
{
int i[5]={-1,-1,-1,-1,-1};//数组存16进制数
int t=0;//数组的下标
int temp;//原数的临时
int qiuhe=0;//求和的结果
int temp2=0;//翻转后的10进制数
int k;//for循环用的

cout<<"before change 10 is :"<<x<<endl; //转换前的10进制是
cout<<"after change 16 is:"; //转换成16进制后是
temp=x;
while(temp>0)
{
i[t]=temp%16;
t++;
temp=temp/16;
}

do
{
t--;
qiuhe=qiuhe+i[t];
if(i[t]>9)
{
switch(i[t])
{
case 10:
cout<<"A\n";
break;
case 11:
cout<<"B\n";
break;
case 12:
cout<<"C\n";
break;
case 13:
cout<<"D\n";
break;
case 14:
cout<<"E\n";
break;
case 15:
cout<<"F\n";
break;
}
}
else
{
cout<<i[t];
}
}while(t>0);

cout<<"\n";
cout<<"sub 16 is:"<<qiuhe<<endl;//16进制下各位和的十进制值是
cout<<"1234 to 4321:";//按位置进行翻转的结果为

for(k=0;k<6;k++)
{
if(i[t]==-1)
{break;
}
else
{
cout<<i[t];
}
temp2=temp2*16+i[t];
t++;
}
cout<<"\n";
cout<<"16 to 10 is :"<<temp2<<endl;//换成10进制为

return 0;
}

void main()
{
int x,y;
cout<<"please input the number(10) you want to change to 16:"<<endl; //请输入你要转换的10进制整数
cin>>x;
while(x<0||x>10000)
{
cout<<"error,please input 0-10000 :"<<endl;
cin>>x;
}
zhuanhuan(x);//转换输出~求和输出~按位置进行翻转并输出~转换成10进制并输出
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值