【C++】將十進制數轉換為十六進制數


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int a;//輸入的數
    int y = 0;//循環中的余數'
    string s = "";
    cin >> a;
    if (a == 0)//比較特殊,單獨處理
    {
        cout << 0;
        return 0;
    }
    while (a > 0)//大於0的數
    {
        y = a % 16;//求余
        if (y < 10)//小於10的余數
        {
            s = char('0' + y) + s;
        }
        else
        {
            s = char('A' - 10 + y) + s;
        }
        a = a / 16;
    }
    cout << s;
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM