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 ...
随机推荐
- 关于left join 和 inner join
今天遇到一个逻辑很复杂的SQL,虽然写出来了,但是并没有完全体会,找了找资料,算是摸清楚了left join和inner join 的实际意义. 感谢PCJIM的文章,写的非常明白,原文地址:http ...
- json在PHP中应用技巧
一.json_encode() 该函数主要用来将数组和对象,转换为json格式.先看一个数组转换的例子: $arr = array ('a'=>1,'b'=>2,'c'=>3,'d' ...
- Python网络编程——获取远程设备的IP地址
有时需要把设备的主机名转换成对应的IP地址,下面是一个简单的操作. import socket def get_remote_machine_info(): # 定义get_remote_machin ...
- Linux下gsoap实现webservice功能
蓝字为关键字,等号=后面为关键字值. 一.介绍 我们用的webservice是根据gsoap编译工具来实现,gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据 ...
- USB CCID协议和PC/SC标准
CCID是USB Chip/Smart Card Interface Devices,也就是USB芯片智能卡接口设备,是USB规范下的一种设备类型.就像HID设备一样,需要参考USB规范来写固件程序来 ...
- XPath与多线程爬虫
XPath是一门在xml中查询信息的语言安装使用XPath 1.安装lxml库 window:pip install lxmllinux:sudo pip install lxml国内安装缓慢,建议到 ...
- matrix67:kmp算法详解
个人认为KMP是最没有必要讲的东西,因为这个东西网上能找到很多资料.但网上的讲法基本上都涉及到“移动(shift)”.“Next函数”等概念,这非常容易产生误解(至少一年半前我看这些资料学习KMP时就 ...
- linux 修改IP, DNS 命令
linux 修改IP, DNS 命令 http://www.cnblogs.com/fighter/archive/2010/03/04/1678007.html 修改DNS [root@localh ...
- c++子类和父类成员函数重名
子类和父类返回值参数相同,函数名相同,有virtual关键字,则由对象的类型决定调用哪个函数. 子类和父类只要函数名相同,没有virtual关键字,则子类的对象没有办法调用到父类的同名函数,父类的同名 ...
- BZOJ AC300题留念