ps:是转成字符串啊!如输入127,输出“7F” 。下面我编的运行结果居然是符号呢??。。。没思路了。q526779789 用c++ thanx
#include <iostream>
#include <string>
using namespace std;
string convert(int,char*);
const max=16;
int main()
{
int x;
char str[max];
do
{cin>>x;
cout<<convert(x, str)<<endl;}while (x!=0);
return 0;
}
string convert(int num, char *hex)
{
int array[max];
int t=num;
while(t%16)
{
for(int j=0;j<max;j++)
array[j]=t%16;//put mod into array
t=t/16;
}
for(int k=0;k<max;k++)
{ int temp=array[k];
if(temp==10)
{hex[k]='A';}
if(temp==11)
{hex[k]='B';}
if(temp==12)
{hex[k]='C';}
if(temp==13)
{hex[k]='D';}
if(temp==14)
{hex[k]='E';}
if(temp==15)
{hex[k]='F';}
else{
hex[k]=array[k];
}
}
string hexx=hex;
return hexx;
}