#include <iostream>
#include <string>
using namespace std;
string ToHex(int dec)
{
unsigned int udec = (dec>=0)?(dec):(-dec), yushu;
int pos = 8;
char Hex[11];
memset(Hex, '0', 9);
Hex[9] = 'H';
Hex[10] = '/0';
do//求余数
{
yushu = udec%16;
Hex[pos--] = (yushu>9)?(yushu+55):(yushu+48);
udec = udec/16;
}
while (udec!=0);
if (Hex[pos+1]>64)//如果最高位是字母,则字符串前面补0
Hex[pos] = '0';
else
++pos;
if (dec<0)//如果是负数,在前面加负号
Hex[--pos] = '-';
return (string(Hex+pos));
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cin>>i;
string sHex = ToHex(i);
cout<<endl<<sHex;
return 0;
}
5251




被折叠的 条评论
为什么被折叠?



