实践中遇到有符号十六进制数需要转化成十进制数,编程如下,如有疏漏,恳请指出。
int hextode(char* hex)
{
if(hex ==NULL)
return 0;
char binary[17]={0};//放二进制字符串
char* hexstr="006C";//一个16进制字符串
//Convert strings to a long-integer value.
long i32=strtol(hexstr,NULL,16);//读取16进制的值
cout<<i32<<endl;
int toint = 0;
int ratio = 1;
if(hexstr[0]>'8')
{
cout<<"此数为负"<<endl;
//Converts an integer to a string
itoa(i32,binary,2);/*输出为二进制*/
for(int i=0;i<16;i++)
cout<<binary[i];
cout<<endl;
cout<<' ';
for(int i=1;i<16;i++) //除第一位外取反
{
binary[i] ='0'+!(binary[i]-'0');
cout<<binary[i];
}
cout<<endl;
int nTakeover = 0;
bool isoverflow = false;
for(int i=15;i>=0;i--) //二进制加1
{
int nsum = binary[i]-'0'+nTakeover; //每位加上进位
if(i==15)
nsum++;
if(nsum==2)
{
if(i == 0)
isoverflow = true;
else
{
nsum-=2;
nTakeover = 1;
binary[i] = '0'+nsum;
}
}
else
{
binary[i] = '0'+nsum;
break;
}
}
for(int i=0;i<16;i++) //取反加一后
{
cout<<binary[i];
}
cout<<endl;
for(int j=15;j>0;--j)
{
toint = toint+ (binary[j]-'0')*ratio;
ratio=ratio*2;
}
toint = toint*(-1);
cout<<toint<<endl;
return toint;
}
else
{
cout<<"此数为正"<<endl;
toint= i32;
cout<<toint<<endl;
return toint;
}
}