htoi的实现
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的实现的更多相关文章
- 练习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 ...
- The C Programming Language (second edition) 实践代码(置于此以作备份)
1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...
- HTTP CHUNKED C实现
C语言不像C#一样有很多很多高度的模块化的东西可以使用,在通讯过程中特别是与http相关的通讯过程中可能要对网站返回的数据做一定处理,而且有不少网站的回应是强制性的,例如向网站请求deflate有个能 ...
- 《c程序设计语言》读书笔记-十六位进制数转十进制
#include <stdio.h> #include <stdio.h> int htoi(char s[]); main() { char s1[] = "10& ...
- 十六进制转十进制 - C
我们经常碰到16进制数转10进制的情况,使用下面的C程序即可完成上述工作. 那么他是怎样的工作原理呢? 6.2.5 十六进制数转换成十进制数 16进制就是逢16进1,但我们只有0~9这十个数字,所以我 ...
- the c programming language 2-3
#include<stdio.h> #define MAXLINELEN 1000 int power(int base,int n) { ; ; ;i<n;i++) answer= ...
- 扩展《C程序设计语言》练习2-3程序通用性
最近开始自学C语言,在看K&R的<C程序设计语言>.练习2-3要求写一个函数,将输入的十六进制数字字符串转换成与之等价的整数值,配套答案没有扩展程序的通用性,所以我就稍微改造改造. ...
- Android中通过进程注入技术改动广播接收器的优先级
前言 这个周末又没有吊事,在家研究了怎样通过进程的注入技术改动广播接收器的优先级.关于这个应用场景是非常多的.并且也非常重要.所以就非常急的去fixed了. Android中的四大组件中有一个广播:B ...
- C程序设计语言(第二版)习题:第二章
这一章习题做着很舒服,毕竟很简单.所以很有感觉. 练习 2-1 Write a program to determine the ranges of char , short , int , and ...
随机推荐
- J2SE 8的Lambda --- functions
functions //1. Runnable 输入参数:无 返回类型void new Thread(() -> System.out.println("In Java8!" ...
- 基于OpenGL编写一个简易的2D渲染框架-02 搭建OpenGL环境
由于没有使用GLFW库,接下来得费一番功夫. 阅读这篇文章前请看一下这个网页:https://learnopengl-cn.github.io/01%20Getting%20started/02%20 ...
- where 命令
在当前目录及path环境变量指定的目录中搜索相应文件 例:where msbuild 查找msbuild的位置
- 为什么NoSql快--磁盘顺序写
数据写入方式 1. update-in-place原地更新 2. append-only btree/copy on write tree顺序文件末尾追加 数据被按照特定方式放置,提升读性能, ...
- Get与Post提交方式的区别
用 curl 测试 post 请求: curl -d "agentCode=RB&startDate=2017-07-01&endDate=2017-09-01& ...
- unity VideoPlayer
Events(事件) started:在调用play()后立刻调用 prepareCompleted:播放器准备完成时 seekCompleted:缓冲完成时
- Java静态初始化,实例初始化以及构造方法
首先有三个概念需要了解: 一.静态初始化:是指执行静态初始化块里面的内容. 二.实例初始化:是指执行实例初始化块里面的内容. 三.构造方法:一个名称跟类的名称一样的方法,特殊在于不带返回值. 我们先来 ...
- SQL 数据库事务 存储过程练习
数据库事务: 数据库事务(Database Transaction) 是指作为单个逻辑工作单元执行的一系列操作. 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源. ...
- Hive—简单窗口分析函数
hive 窗口分析函数 : jdbc:hive2:> select * from t_access; +----------------+---------------------------- ...
- HttpClient 发送 HTTP、HTTPS
首先说一下该类中需要引入的 jar 包,apache 的 httpclient 包,版本号为 4.5,如有需要的话,可以引一下. 代码 import org.apache.commons.io ...