16진수를 나타내는 문자열을 입력하고 10진수로 변환하는 정수 출력을 입력하십시오.
2127 단어 《알고리즘과 데이터 구조》의 실천
/***********************************************************************************
16 10
1. 0x 0x
2. 32 , 16 FFFF, 4 , , 0
3. strtol 16
************************************************************************************/
#include
#include
#include
/* */
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;
}
else
temp=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
",tempoint);
temp2=modifbit(tempoint);
//printf("count=%s
",temp2);
tempoint=temp2;
for(i=3;i>=0;i--)
{
string[j++]=*(tempoint+i);
}
string[4]='\0' ;
printf("string1=%s
",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
",Dec);
return Dec;
}
void main()
{
//printf("the Hextoint is %d
",strtol("0xff",NULL,16));
printf("the Hextoint is %d
",Hexstoinit("fff"));
}