转自:http://www.cnblogs.com/cobbliu/archive/2012/08/25/2656176.html

atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C语言实现的atoi和itoa:

1, atoi (将字符串转换为数字)

原型: int atoi(const char *nptr);

函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。

#include <stdio.h>
#include <assert.h>
static int atoi(const char* str)
{
int result = 0;
int sign = 0;
assert(str != NULL);
// proc whitespace characters
while (*str==' ' || *str=='\t' || *str=='\n')
++str; // proc sign character
if (*str=='-')
{
sign = 1;
++str;
}
else if (*str=='+')
{
++str;
} // proc numbers
while (*str>='0' && *str<='9')
{
result = result*10 + *str - '0';
++str;
} // return result
if (sign==1)
return -result;
else
return result;

2. itoa (将数字转换成字符串)

char *itoaint value, char *string,int radix);

  原型说明:

  value欲转换的数据。

  string:目标字符串的地址。

  radix:转换后的进制数,可以是10进制、16进制等

char *itoa(int val, char *buf, unsigned radix)
{
char *p;
char *firstdig;
char temp;
unsigned digval;
p = buf;
if(val <0)
{
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p;
do{
digval = (unsigned)(val % radix);
val /= radix; if (digval > 9)
*p++ = (char)(digval - 10 + 'a');
else
*p++ = (char)(digval + '0');
}while(val > 0); *p-- = '\0 ';
do{
temp = *p;
*p = *firstdig;
*firstdig = temp;
--p;
++firstdig;
}while(firstdig < p);
return buf;
}

atoi 和 itoa的更多相关文章

  1. 面试:atoi() 与 itoa()函数的内部实现(转)

    原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918   #include <stdio.h> #include < ...

  2. atoi 和 itoa的实现

    atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C语言实现的atoi和itoa: 1, atoi 原型: int atoi(const char *nptr); 函数说明: 参数np ...

  3. atoi、itoa,strcpy,strcmp,memcpy等实现

    原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http ...

  4. c常用函数-atoi 和 itoa

    atoi 和 itoa atoi的功能是把一个字符串转为整数 Action(){ int j; char *s=""; j = atoi(s); lr_output_message ...

  5. 工作的准备:atoi,itoa,strcpy,memcpy,strcmp,二分查找,strcat

    对常见的几个函数,周末没事写写,绝对是笔试面试中非频繁,前面n届学长无数次强调了,大家就别怀疑了.从今天开始,每天10道题. int atoi(const char* str) { if(str==N ...

  6. C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...

  7. atoi 和itoa用法

    1.itoa 在linux下没有itoa这个函数 原型:char  *itoa(int   value,char   *string,int   radix)                   用法 ...

  8. c语言实现atoi和itoa函数。

    首先看atoi函数: C语言库函数名: atoi 功 能: 把字符串转换成整型数. 名字来源:ASCII to integer 的缩写. 原型: int atoi(const char *nptr); ...

  9. c++实现atoi()和itoa()函数(字符串和整数转化)

    (0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...

随机推荐

  1. NFC TI TRF7970A Breakout Board for BusPirate or other HW

    http://dangerousprototypes.com/forum/viewtopic.php?f=19&t=3187 Just a news about a new Hardware ...

  2. Read UNIQUE ID and flash size method for stm32

    /* 读取stm32的unique id 与 flash size */ /* func: unsigned int Read_UniqueID_Byte(unsigned char offset) ...

  3. [js插件]JqueryUI日期插件

    引言 之前使用jqueryUi中的弹出框做了一个可拖拽的弹出登录框,也顺便将里面的常用的日期插件和文本框智能提示插件,也学习了一下. 使用方法 首先在项目中引入以下文件: <!-- 日期插件 默 ...

  4. .NET Versioning and Multi-Targeting - .NET 4.5 is an in-place upgrade to .NET 4.0

    Say what you will about the past ridiculousness of .NET Framework versioning, since the confusion of ...

  5. ODS与数据仓库

    数据仓库是目前主要的数据存储体系.数据仓库之增W.H.Inmon认为,数据仓库是指支持管理决策过程的.面向主题的.集成的.随时间而变的.持久的数据的集合.简单地说,一个数据仓库就一个自数据库的商业应用 ...

  6. xml布局内容总结(一)--Android

    关于安卓项目中xml的使用非常多.为了达到一些好的UI效果.须要对xml比較熟练.会使用非常多的小技巧,本人准备对这些小技巧进行整理和总结,希望进行分享和交流. 关于weight的使用,因为weigh ...

  7. Computer Vision Tutorials from Conferences (2) -- ECCV

    ECCV 2012 (http://eccv2012.unifi.it/program/tutorials/) Vision Applications on Mobile using OpenCVGa ...

  8. MySQL 中文乱码解决

    測试环境:服务端和client均为win7,MySql数据库.表字符集为utf-8,字段字符集与表一致. 1.使用mysql命令进行操作时的乱码问题解决. (1)设置当前字符集 set names g ...

  9. 【crontab】“bad minute”及“errors in crontab file, can't install”错误处理

    今天有朋友提到,在使用crontab定制后台定时备份任务时报出“bad minute”及“errors in crontab file, can't install”错误.经确认,根本原因是cront ...

  10. Javaee项目经验须知

    Java的主要应用领域就是企业级的项目开发!具体要点(09年,那一年我去面试,被拒了几次,想起来还不错!他锻炼了我的心理素质,让我体会到很多,笑一个吧!): 1.掌握项目开发的基本步骤 2.具备极强的 ...