首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

十进制转为16进制

2012-10-19 
10进制转为16进制#include iostream#include stringusing namespace stdchar ToHexChar(int n){retur

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

热点排行