天天看點

十六進制轉換十進制

#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;

}