Linux⼗六进制转换⼗进制的函数
这⾥记⼀句,如果是long型或unsigned long型的数,可以直接printf("%ul\%l");的⽅式直接输出出来。或者可以使⽤sprintf (buf,"%x\%ul\%l",dex);这种⽅式将⼗六进制数存放在⼀个数组⾥,然后再使⽤如下的函数。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int toi(char s[])
{
  int i;
  int n = 0;
  if (s[0] == '0' && (s[1]=='x' || s[1]=='X'))
  {
    i = 2;
  }
  else
  {
    i = 0;
  }
  for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i)
  {
printf输出格式linux    if (tolower(s[i]) > '9')
    {
      n = 16 * n + (10 + tolower(s[i]) - 'a');
    }
    else
    {
      n = 16 * n + (tolower(s[i]) - '0');
    }
  }
  return n;
}
int main(){
  printf("%d",toi("0xff"));
}