天天看点

十六进制转换十进制

#include <iostream>

#include <stdio.h>

#include <list>

using namespace std; 

char p[2]; 

//char Exchange(int number)

//功能:将输入的数字转为对应的十六进制字符 

char Exchange(int number)

{

if(number < 10)

{

sprintf(p,"%d",number);//将int型数据写入p字符串中 

return p[0];

}

else

{

switch(number)

{

case 10:return 'A';

case 11:return 'B';

case 12:return 'C';

case 13:return 'D';

case 14:return 'E';

case 15:return 'F';

}

}

}

int main() 

{

list<char>exp;

unsigned int num;//记录待转化的数 

                //unsigned int 0 ~ 4294967295

cin>>num;

if(num == 0)//输入为0的情况 

{

cout<<num<<endl;

}

else//输入大于0的情况 

{

int re;//记录余值 

while(num > 0)

{

re = num%16;

exp.push_front(Exchange(re)); 

num /= 16;  

}

int size = exp.size();

while(size--)//输出

{

cout<<exp.front();

exp.pop_front();

}

}

return 0;

}