您的位置:首页 > 其它

进制转换

2014-08-27 23:14 369 查看
#include <iostream>

using namespace std;

//十进制转16进制
char tohex(int num)
{
if(num>=10&&num<=15)
{
return 'A'+num-10;
}
return num+'0';
}

void func(int input,char *buf)
{
int num,n=0;
char temp;
while(input)
{
num=input%16;
buf[n++]=tohex(num);
input/=16;
}
buf
='\0';
for(int i=0,j=n-1;i<j;i++,j--)
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}

//16进制转10进制
int toNum(char hex)
{
int n=0;
if(hex>='a'&&hex<='f')
{
n=hex-'a'+10;
}
else if(hex>='A'&&hex<='F')
{
n=hex-'A'+10;
}
else
n=hex-'0';
return n;
}

int func2(char *input)
{
int num,result=0;
while(*input!='\0')
{
num=toNum(*input);
result=result*16+num;
input++;
}
return result;
}

int main()
{
int a=58;
//char *buf=new char[5];
//func(a,buf);
//cout<<buf;
cout<<func2("3a");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: