c语言实现atoi和itoa函数。
首先看atoi函数:
int atoiOwn(const char *a)
{
int val=;
bool b_plus=true;//判断符号
switch(*a) //过滤符号
{
case '+':
a++;
break;
case '-':
a++;
b_plus=false;
break;
default:
break;
} while(*a>='0'&&*a<='9') //可以用isdigit判断。
{
val=val*+(*a-'');
a++;
}
if(!b_plus)
val=-val;
return val;
}
int main()
{ char a[];
while(scanf("%s",a)!=EOF)
{
int ret=atoiOwn(a);
printf("%d\n",ret);
}
}
char * itoa ( int value, char * str, int base );
Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.
If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, valueis always considered unsigned.
str should be an array long enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.
A standard-compliant alternative for some cases may be sprintf:
- sprintf(str,"%d",value) converts to decimal base.
- sprintf(str,"%x",value) converts to hexadecimal base.
- sprintf(str,"%o",value) converts to octal base.
以下的代码只是模拟了部分功能:
#include<stdio.h>
void itoa(int value, char *str)
{
if (value < ) //如果是负数,则str[0]='-',并把value取反(变成正整数) {
str[] = '-';
value = -value;
}
int i,j;
for(i=; value > ; i++,value/=) //从value[1]开始存放value的数字字符,不过是逆序,等下再反序过来 str[i] = value%+''; //将数字加上0的ASCII值(即'0')就得到该数字的ASCII值 for(j=i-,i=; j-i>=; j--,i++) //将数字字符反序存放 {
str[i] = str[i]^str[j];
str[j] = str[i]^str[j];
str[i] = str[i]^str[j];
}
if(str[] != '-') //如果不是负数,则需要把数字字符下标左移一位,即减1 {
for(i=; str[i+]!='\0'; i++)
str[i] = str[i+];
str[i] = '\0';
}
} void main()
{
int value = -;
char str[10] = {'\0'}; //记得把str全填充为'\0' 这个错误,其实这样赋值只是把第
1个元素赋值为\0,后面的都默认用\0填充,如果是char str[10]={'1'};
只有第一个为‘1’,后面都是\0.但千万不要以为写成char str[10];不赋值也可以。这样写里面的内容是乱的。
itoa(value, str);
printf("The result is:%s\n", str);
}
另一种写法:
void intToStr(int num,char str[])
{
int i=,j=,isNeg=;
if(num<)
{
num*=-;
isNeg=;
}
do{
str[i++]=(num%)+'';
num/=;
}while(num); if(isNeg)
str[i++]='-'; //reverse the characher;
for(int m=,n=i-;m<n;m++,n--)
swap(str[m],str[n]); str[i]='\0';
}
为什么写成:
do{
str[i++]=(num%10)+'0';
num/=10;
}while(num);
而不是
while(num)
{
}
因为当输入为0时,while(num)一次都不会执行,导致最后输出的是空串。因为至少要执行一次,所以用do while.
更好的办法:
http://blog.csdn.net/solstice/article/details/5139302
http://stackoverflow.com/questions/3440726/what-is-the-proper-way-of-implementing-a-good-itoa-function
参考;http://www.cppblog.com/lizhongxu2008/archive/2009/02/11/73470.html
许多实现:http://www.jb.man.ac.uk/~slowe/cpp/itoa.html#dev
/**
* Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C":
*/
void strreverse(char* begin, char* end) {
char aux;
while(end>begin)
aux=*end, *end--=*begin, *begin++=aux;
}
void itoa(int value, char* str, int base) {
static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char* wstr=str;
int sign;
// Validate base
if (base< || base>){ *wstr='\0'; return; }
// Take care of sign
if ((sign=value) < ) value = -value;
// Conversion. Number is reversed.
do *wstr++ = num[value%base]; while(value/=base);
if(sign<) *wstr++='-';
*wstr='\0';
// Reverse string
strreverse(str,wstr-);
}
/**
* Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C"
* with slight modification to optimize for specific architecture:
*/
void strreverse(char* begin, char* end) {
char aux;
while(end>begin)
aux=*end, *end--=*begin, *begin++=aux;
}
void itoa(int value, char* str, int base) {
static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char* wstr=str;
int sign;
div_t res;
// Validate base
if (base< || base>){ *wstr='\0'; return; }
// Take care of sign
if ((sign=value) < ) value = -value;
// Conversion. Number is reversed.
do {
res = div(value,base);
*wstr++ = num[res.rem];
}while(value=res.quot);
if(sign<) *wstr++='-';
*wstr='\0';
// Reverse string
strreverse(str,wstr-);
}
c语言实现atoi和itoa函数。的更多相关文章
- 面试:atoi() 与 itoa()函数的内部实现(转)
原 面试:atoi() 与 itoa()函数的内部实现 2013年04月19日 12:05:56 王世晖 阅读数:918 #include <stdio.h> #include < ...
- atoi()和itoa()函数详解以及C语言实现
atoi()函数 atoi()原型: int atoi(const char *str ); 函数功能:把字符串转换成整型数. 参数str:要进行转换的字符串 返回值:每个函数返回 int 值,此值 ...
- c++实现atoi()和itoa()函数(字符串和整数转化)
(0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...
- atoi和itoa函数的实现方法
atoi的实现: #include<iostream> using namespace std; int atio1(char *s) { int sign=1,num=0; if(*s= ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
- [置顶]
C语言itoa()函数和atoi()函数详解(整数转字符C实现)
头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...
- 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)
本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...
随机推荐
- 纯CSS写九宫格样式,高宽自适应正方形
<!DOCTYPE html><html> <head> <meta charset="utf-8"> ...
- 经典排序算法及python实现
今天我们来谈谈几种经典排序算法,然后用python来实现,最后通过数据来比较几个算法时间 选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据 ...
- 「OC」block 和 protocol
一.block (一)简介 block 是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,block 可以在任何时候执行.在多线程.异步任务.集合遍历.集合排序.动 ...
- Java中 hashCode()方法详解
先来看下Object源码里hashcode方法: /** * Returns a hash code value for the object. This method is * s ...
- linux系统调用和库函数调用的区别(转)
Linux下对文件操作有两种方式:系统调用(system call)和库函数调用(Library functions).可以参考<Linux程序设计>(英文原版为<Beginning ...
- Django内置template标签
html过滤{% autoescape on|off %} {{body}} {% endautoescape %} 注释{% comment %} {% endcomment %} csrf攻击 { ...
- C语言之猜数字游戏
猜数字游戏 猜数字游戏是以前功能机上的一款益智游戏,计算机会根据输入的位数随机分配一个符合要求的数据,计算机输出guess后便可以输入数字,注意数字间需要用空格或回车符加以区分,计算机会根据输入信息给 ...
- FPGA知识大梳理(二)verilogHDL语法入门(1)
此文是写给0基础学习者,也是对自己知识点总结水平的考验. 对于有C基础的人来说,学习verilog应该是轻而易举 —— 类比法学习. 第一步:格式. 对于C来说我们前面会写 ‘include“std ...
- QT图片旋转
目前发现有两种方法,如下: 1.使用QPixmap的transformed函数旋转,这个函数默认是以图片中心为旋转点,不能随意设置旋转点,使用如下: QMatrix leftmatrix; leftm ...
- matrix67:kmp算法详解
个人认为KMP是最没有必要讲的东西,因为这个东西网上能找到很多资料.但网上的讲法基本上都涉及到“移动(shift)”.“Next函数”等概念,这非常容易产生误解(至少一年半前我看这些资料学习KMP时就 ...