//将正数字符串转化为相应的正数
float GetNum(const char *nptr)
{
int c; /* current char */
long inttotal; /* current total */
float fractotal;
inttotal = 0;
/* skip whitespace */
while ( isspace((int)(unsigned char)*nptr) )
++nptr;
c = (int)(unsigned char)*nptr++;
while (isdigit(c))
{//获取整数
inttotal = 10 * inttotal + (c - '0'); /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}
if (*nptr)
{//获取小数
int i;
fractotal=0;
c = (int)(unsigned char)*nptr++;
for (i=1;isdigit(c);i++)
{
fractotal = fractotal + pow(0.1,i) * (c - '0'); /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}
return inttotal+fractotal;
}
return inttotal;
}
//将十六进制数转化为十进制数
float HexToDec(const char *nptr)
{
int c; /* current char */
long inttotal; /* current total */
float fractotal;
inttotal = 0;
/* skip whitespace */
while ( isspace(*nptr) )
++nptr;
c = *nptr;
while (c && c!='.')
{//获取整数
if (c<'A')
inttotal = 16 * inttotal + (c - '0');
else if (c>='A' && c<'a')
inttotal = 16 * inttotal + 10 + (c - 'A');
else
inttotal = 16 * inttotal + 10 + (c - 'a');
c = *(++nptr); /* get next char */
}
if (*nptr)
{//获取小数
int i;
fractotal=0;
c = *(++nptr);
for (i=1;c;i++)
{
if (c<'A')
fractotal = fractotal + pow(16,-i) * (c - '0');
else if (c>='A' && c<'a')
fractotal = fractotal + pow(16,-i) * (c - 'A');
else
fractotal = fractotal + pow(16,-i) * (c - 'a');
c = *(++nptr); /* get next char */
}
return inttotal+fractotal;
}
return inttotal;
}
//将十进制数转化为十六进制数
void F(int n)
{
if ( n )
{
F(n/16);
if (n%16 < 10) printf("%d",n%16);
else printf("%c",(n%16)-10+'A');
}
}