close

 

#include <iostream>
#include <string>

using namespace std;

char DecToHex (unsigned int n)
{
    char hex[] = {'0', '1', '2', '3','4','5'
                 ,'6','7','8','9','a','b','c'
                 ,'d','e','f'
                 };
    return hex[n] ;
}

class Solution 
{
public :
    string toHex (int num )
    {
        unsigned int unNum = (int) num ;
        string result ;

        do 
        {
            result = DecToHex(unNum % 16) + result ;
            unNum /= 16 ;
        } while (unNum>0);
        return result ;
    }
};

int main()
{
    Solution Test ;
    int n ;
    string strNumber ;
    while (cin>>n)
    {
        strNumber = Test.toHex(n) ;
        cout << "Decimal ( " << n <<" ) to Hexadecimal is : " << strNumber << endl ;
    }
}

arrow
arrow
    創作者介紹
    創作者 Eric 的頭像
    Eric

    一個小小工程師的心情抒發天地

    Eric 發表在 痞客邦 留言(0) 人氣()