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. 怎么才能将文件流或者图片转化为base64,传到前台展示

    图片转化为base64,传到前台展示 public String getBase64(){ String imgStr = ""; try { File file = new Fi ...

  2. 转载:mysql binlog同步redis

    ref: https://wenku.baidu.com/view/5d9d04ac6394dd88d0d233d4b14e852458fb39c4.html

  3. Apache Hive 执行HQL语句报错 ( 10G )

    # 故障描述: hive > , ) as uuid, count(distinct(request_body["uuid"])) as count from log_bft ...

  4. VCL编写笔记整理

    unit hzqEdit1; interface uses  SysUtils, Classes, Controls, StdCtrls; type  TEditDataType = (dtpStri ...

  5. Java含有Date的对象序列化网络传输

    与短信接口对接时,Date从我这边传输以及在短信平台接收后转换出了问题 传入一个TemplateRequest对象 Feign接口 将含有Date的将要传输的TemplateRequest加上@Req ...

  6. knn手写识别

    import numpy as np import operator import os #KNN算法 def knn(k,testdata,traindata,labels):#(k,测试样本,训练 ...

  7. 大型运输行业实战_day04_2_高级查询

    1.高级查询最总效果 2.高级查询步骤 2.1页面输入框显示 开始车站:<input id="startStation" type="text" valu ...

  8. dubbo通信协议

    对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况,本文参考dubbo官方文档 http://dubbo.io/User+Guide-zh.htm dubbo共支持如下几种通信协议: ...

  9. spring 每个jar的作用

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...

  10. Spring简单获得实体类的实例, 使用ApplicationContext()方法的几点注意事项

    今天接触了Spring的初步用法, 感觉跟实例化实体类没啥区别, 像这种简单的代码还不如直接实例化来的方便, 这样使用Spring的话总共需要三个文件 第一个当然是一个实体类了, 定义好属性, get ...