您的位置:首页 > 其它

10进制转为16进制

2012-10-14 12:26 543 查看
#include <iostream>
#include <string>
using namespace std;
char ToHexChar(int n)
{
    return n<10 ? n+'0' : n-10+'A';
}
string foo(unsigned int n)
{
    char t, buff[32]={'0','x', 0};
    int i = 2, j = 2;
    do buff[i++] = ToHexChar(n%16); while (n/=16);
    for (--i; j < i; ++j, --i)
        t = buff[i], buff[i] = buff[j], buff[j] = t;
    return buff;
}

int main()
{
    unsigned int n;
    while (cin >> n) cout << foo(n) << endl;
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: