200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 输入一个表示十六进制的字符串 转换为十进制的整数输出。

输入一个表示十六进制的字符串 转换为十进制的整数输出。

时间:2018-08-27 21:53:30

相关推荐

输入一个表示十六进制的字符串 转换为十进制的整数输出。

/***********************************************************************************将16进制的字符串转换成10进制1.如果存在0x 必需将 0x 剔除2.该程序假定是在32位机器上,故16进制为FFFF,不足4个字符串,修正一下,前面空格填03.也可以是用strtol 直接完成字符串和16进制的转换************************************************************************************/#include<stdio.h>#include<string.h>#include<math.h>/*计算字符串是否有四个字节*/int GetLeghtofHEX(char *p){int count=0;char *temp =p;while(*temp++!='\0')count++;return count ;}/*如果不足四个字节,前4-count位填0*/char *modifbit(char *p){ char tab[5];int i=0,j=0;char * temp;int count=GetLeghtofHEX(p);if (count!=4){for(i=0;i<4-count;i++)tab[i]='0';while(i<4){tab[i]=*(p+j);j++;i++;}tab[i]='\0';temp = tab;}elsetemp=p;return temp;}/*利用16进制和10进制转换公式进行换算*/int Hexstoinit(char *p){int i,j=0;char string[5]="0000";int Dec=0,temp;char *tempoint=p;char *temp2;if(strstr(p,"0x")){tempoint=strstr(p,"0x")+2;}printf("string=%s\n",tempoint);temp2=modifbit(tempoint);//printf("count=%s\n",temp2);tempoint=temp2;for(i=3;i>=0;i--){string[j++]=*(tempoint+i);}string[4]='\0' ;printf("string1=%s\n",string);for(j=0;j<4;j++){if(string[j]<='9'){temp=string[j]-'0';}else if(string[j]=='a'||string[j]=='A')temp=10;else if(string[j]=='b'||string[j]=='B')temp=11;else if(string[j]=='c'||string[j]=='C')temp=12;else if(string[j]=='d'||string[j]=='D')temp=13;else if(string[j]=='e'||string[j]=='E')temp=14;else if(string[j]=='f'||string[j]=='F')temp=15;Dec+=temp*pow(16,j);}printf("string=%d\n",Dec);return Dec;}void main(){//printf("the Hextoint is %d\n",strtol("0xff",NULL,16));printf("the Hextoint is %d\n",Hexstoinit("fff"));}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。