zoukankan      html  css  js  c++  java
  • 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)
      {
        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"));
    }

  • 相关阅读:
    css颜色表示法&颜色表
    css单位
    DOM与BOM
    position定位
    grid layout
    Linux禁止Ping方法
    tracert(traceroute)与ping
    服务器负载均衡技术的原理
    Struts2与webx的比较
    SpringAOP的原理
  • 原文地址:https://www.cnblogs.com/wanhl/p/2958806.html
Copyright © 2011-2022 走看看