读书人

十进制转为16进制

发布时间: 2012-10-19 16:53:35 作者: rapoo

10进制转为16进制

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

读书人网 >编程

热点推荐