htoi的实现

 /*************************************************************************
> File Name: htoi.c
> Author: ChenPeng
> Mail:479103815@qq.com
> Blog: http://www.cnblogs.com/cpsmile/
> Created Time: Fri 03 Apr 2015 07:22:15 PM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h> #define YES 1
#define NO 0 /*将十六进制数字组成的字符串转换为与之等价的整型值*/
int htoi(char *str)
{
int hexdigit;//记录每一个十六进制数对应的十进制数
int i;//工作指针
int ishex;//是否是有效的十六进制数
int n;//返回的十进制数 i= ;
if(str[i] == '')
{
++i;
if(str[i] == 'x' || str[i] == 'X')
++i;
}
n = ;
ishex = YES;
for(; ishex == YES; ++i)
{
if(str[i] >= '' && str[i] <= '')
hexdigit = str[i] - '';
else if(str[i] >= 'a' && str[i] <= 'f')
hexdigit = str[i] - 'a' + ;
else if(str[i] >= 'A' && str[i] <= 'F')
hexdigit = str[i] - 'A' + ;
else
ishex = NO;
if(ishex == YES)
n = * n + hexdigit;
}
return n;
} int main()
{
char str[] = "";
int val;
while(printf("Please enter a hex string:\n"),scanf("%s",str) == )
{
val = htoi(str);
printf("整数是:%d\n",val);
}
return ;
}

htoi的实现的更多相关文章

  1. 练习2-3:十六进制数字字符串转换为等价整型值,字符串允许包含的数字包括:0~9、a~f、A~F、x、X(C程序设计语言 第2版)

    #include <stdio.h> #include <string.h> #include <math.h> int htoi(char s[]){ unsig ...

  2. The C Programming Language (second edition) 实践代码(置于此以作备份)

    1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...

  3. HTTP CHUNKED C实现

    C语言不像C#一样有很多很多高度的模块化的东西可以使用,在通讯过程中特别是与http相关的通讯过程中可能要对网站返回的数据做一定处理,而且有不少网站的回应是强制性的,例如向网站请求deflate有个能 ...

  4. 《c程序设计语言》读书笔记-十六位进制数转十进制

    #include <stdio.h> #include <stdio.h> int htoi(char s[]); main() { char s1[] = "10& ...

  5. 十六进制转十进制 - C

    我们经常碰到16进制数转10进制的情况,使用下面的C程序即可完成上述工作. 那么他是怎样的工作原理呢? 6.2.5 十六进制数转换成十进制数 16进制就是逢16进1,但我们只有0~9这十个数字,所以我 ...

  6. the c programming language 2-3

    #include<stdio.h> #define MAXLINELEN 1000 int power(int base,int n) { ; ; ;i<n;i++) answer= ...

  7. 扩展《C程序设计语言》练习2-3程序通用性

    最近开始自学C语言,在看K&R的<C程序设计语言>.练习2-3要求写一个函数,将输入的十六进制数字字符串转换成与之等价的整数值,配套答案没有扩展程序的通用性,所以我就稍微改造改造. ...

  8. Android中通过进程注入技术改动广播接收器的优先级

    前言 这个周末又没有吊事,在家研究了怎样通过进程的注入技术改动广播接收器的优先级.关于这个应用场景是非常多的.并且也非常重要.所以就非常急的去fixed了. Android中的四大组件中有一个广播:B ...

  9. C程序设计语言(第二版)习题:第二章

    这一章习题做着很舒服,毕竟很简单.所以很有感觉. 练习 2-1 Write a program to determine the ranges of char , short , int , and ...

随机推荐

  1. SourceTree使用方法

    一 .SourceTree简介 SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作.同时它也是Mercurial和Subve ...

  2. android MD5 SHA1

    参考文章: AndroidStudio 中怎样查看获取MD5和SHA1值(应用签名)(https://www.cnblogs.com/zhchoutai/p/7102516.html) 使用 java ...

  3. LInux下LD_LIBRARY_PATH的作用与设置

    LD_LIBRARY_PATH环境变量主要是用于指定动态链接器(Id)超早elf可执行文件运行时所依赖的动态库(so)的路径,其内容是以冒号分隔的路径列表. Id链接器优先在该变量设置的路径中查找,若 ...

  4. MIME(Multipurpose Internet Mail Extensions-多用途互联网邮件扩展)

    MIME MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时 ...

  5. kernel TCP time wait bucket table overflow

    # 故障描述 有一个需求是实时分析API接口访问日志,提取token去数据库查询对应的uid,然后收集一些指标存入到hbase中. 当程序执行一会后会被系统杀死 Killed ! # 故障排查 .CP ...

  6. How to Pronounce SAY, SAYS, and SAID

    How to Pronounce SAY, SAYS, and SAID Share Tweet Share Tagged With: Comparison I’ve noticed many non ...

  7. mysql in 过滤 解决转义问题

    IF(headUser!='',instr(concat(',',headUser,','),concat(',',cr.headUser,',')),TRUE);

  8. scala--函数式对象

    函数式对象 这次写点关于函数式对象的吧 class Rational(n:Int, d:Int) { // n,d 为类参数,scala会创造出同样带有这两个参数的主构造器.如果这个类没有主体,可以不 ...

  9. Oracle VM VirtualBox做好虚拟硬盘后,如何进一步修改虚拟硬盘的大小

    以管理员身份打开, 命令提示符窗口,然后利用命令cd进入Oracle VM VirtualBox安装目录,如下图: 我进入了Oracle VM VirtualBox安装目录:D:\Program Fi ...

  10. WebDriverException:Element is not clickable at point - selenium执行过程中遇到的相关报错

    Element is not clickable at point (x, y) 这段可以忽略:本文来自 https://www.cnblogs.com/lozz/p/9947430.html 引起这 ...