博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux 十六进制转换十进制的函数
阅读量:4709 次
发布时间:2019-06-10

本文共 707 字,大约阅读时间需要 2 分钟。

这里记一句,如果是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"));
}

转载于:https://www.cnblogs.com/wanhl/archive/2013/03/14/2958806.html

你可能感兴趣的文章
JS Map对象
查看>>
变量和常量
查看>>
通信项目开发的原理与范例
查看>>
sql数据库(查询)
查看>>
GetMessage 和 PeekMessage 区别
查看>>
DataTable中如何去除重复的项 (获得某个字段中的不重复项)
查看>>
SQL yog过期后教你怎么让他不过期
查看>>
17-MySQL-Ubuntu-数据表的查询-分页(六)
查看>>
Leetcode-441 Arranging Coins
查看>>
SharedPreference.Editor的apply和commit方法异同
查看>>
【转】Caused by: java.lang.NoClassDefFoundError: android.support.v7.gridlayout.R$dimen 异常解决方法...
查看>>
Oracle 数据库字典 sys.col$ 表中关于type#的解释
查看>>
手动写的第一个eChart代码
查看>>
Vue数据绑定隐藏的神坑....
查看>>
邮件欺诈与SPF防御
查看>>
第18月第16天 statusBar
查看>>
第28月第24天 requestSerializer
查看>>
LeetCode Largest Divisible Subset
查看>>
LeetCode 407. Trapping Rain Water II
查看>>
Lua性能优化
查看>>