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 ...
随机推荐
- RESTful介绍和使用教程
出自:https://blog.csdn.net/x541211190/article/details/81141459 一.REST起源REST(Representational State Tra ...
- 前端开发-2-HTML
1.开发环境 市面上有很多的HTML编辑器可以选择,常见的Hbuild.Sublime Text.Dreamweare都可以用来开发HTML. 当然PyCharm也支持HTML开发. 2.文件后缀名规 ...
- DataTable转换成实体
public static class DataTableToEntity { /// <summary> /// 将DataTable数据源转换成实体类 /// </summary ...
- C++中怎么创建类对象
在C++里,有两种方法创建对象:方法一:ClassName object(param);这样就声明了一个ClassName类型的object对象,C++会为它分配足够的存放对象所有成员的存储空间.注意 ...
- Haskell语言学习笔记(31)ListT
Control.Monad.Trans.List 标准库中的 ListT 的实现由于有 bug,已经被废弃. list-t 模块 这里使用 list-t 模块中的 ListT. list-t 模块需要 ...
- Mysql通过SQL脚本复制表
create table if not exists t_dest like t_src 其中,t_src是要复制的原始表,t_dest是要创建的新表.
- mysql 用户及赋予权限
查询用户: use mysql; select host,user from mysql.user; 创建用户: create user 'mhc'@'%' identified by 'mhc.12 ...
- HTTP Response Code 中文详解
引自:https://blog.csdn.net/lplj717/article/details/70053560 1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应 ...
- 绘制pathway富集散点图
本文转载自http://www.omicshare.com/forum/forum.php?mod=viewthread&tid=146&extra=page%3D1%26filter ...
- Oracle to_date()函数的用法《转载》
to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明, 原文地址:http://database.51cto.com/art ...