首先看atoi函数:

C语言库函数名: atoi
功 能: 把字符串转换成整型数。
名字来源:ASCII to integer 的缩写。
原型: int atoi(const char *nptr);
函数说明: 参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零,(意思是遇到非数字或结束符就停止)
头文件: #include <stdlib.h>
输入: -123ab;结果为-123;
输入:abc,输出0.
实现:
  1. int atoiOwn(const char *a)
  2. {
  3. int val=;
  4. bool b_plus=true;//判断符号
  5. switch(*a) //过滤符号
  6. {
  7. case '+':
  8. a++;
  9. break;
  10. case '-':
  11. a++;
  12. b_plus=false;
  13. break;
  14. default:
  15. break;
  16. }
  17.  
  18. while(*a>='0'&&*a<='9') //可以用isdigit判断。
  19. {
  20. val=val*+(*a-'');
  21. a++;
  22. }
  23. if(!b_plus)
  24. val=-val;
  25. return val;
  26. }
  27. int main()
  28. {
  29.  
  30. char a[];
  31. while(scanf("%s",a)!=EOF)
  32. {
  33. int ret=atoiOwn(a);
  34. printf("%d\n",ret);
  35. }
  36. }
char *itoa(int value, char *string, int radix);
int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等
  1. char * itoa ( int value, char * str, int base );
Convert integer to string (non-standard function)

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 basevalueis 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.

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

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.

以下的代码只是模拟了部分功能:

  1. #include<stdio.h>
  2. void itoa(int value, char *str)
  3. {
  4. if (value < ) //如果是负数,则str[0]='-',并把value取反(变成正整数)
  5.  
  6. {
  7. str[] = '-';
  8. value = -value;
  9. }
  10. int i,j;
  11. for(i=; value > ; i++,value/=) //从value[1]开始存放value的数字字符,不过是逆序,等下再反序过来
  12.  
  13. str[i] = value%+''; //将数字加上0的ASCII值(即'0')就得到该数字的ASCII值
  14.  
  15. for(j=i-,i=; j-i>=; j--,i++) //将数字字符反序存放
  16.  
  17. {
  18. str[i] = str[i]^str[j];
  19. str[j] = str[i]^str[j];
  20. str[i] = str[i]^str[j];
  21. }
  22. if(str[] != '-') //如果不是负数,则需要把数字字符下标左移一位,即减1
  23.  
  24. {
  25. for(i=; str[i+]!='\0'; i++)
  26. str[i] = str[i+];
  27. str[i] = '\0';
  28. }
  29. }
  30.  
  31. void main()
  32. {
  33. int value = -;
  34. char str[10] = {'\0'}; //记得把str全填充为'\0' 这个错误,其实这样赋值只是把第
  35. 1个元素赋值为\0,后面的都默认用\0填充,如果是char str[10]={'1'};
    只有第一个为‘1’,后面都是\0.但千万不要以为写成char str[10];不赋值也可以。这样写里面的内容是乱的。

  36. itoa(value, str);
  37. printf("The result is:%s\n", str);
  38. }

另一种写法:

  1. void intToStr(int num,char str[])
  2. {
  3. int i=,j=,isNeg=;
  4. if(num<)
  5. {
  6. num*=-;
  7. isNeg=;
  8. }
  9. do{
  10. str[i++]=(num%)+'';
  11. num/=;
  12. }while(num);
  13.  
  14. if(isNeg)
  15. str[i++]='-';
  16.  
  17. //reverse the characher;
  18. for(int m=,n=i-;m<n;m++,n--)
  19. swap(str[m],str[n]);
  20.  
  21. str[i]='\0';
  22. }

为什么写成:

  1. do{
  2. str[i++]=(num%10)+'0';
  3. num/=10;
  4. }while(num);
    而不是
    while(num)
    {
      
  5.  
  6. }
    因为当输入为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

  1. /**
  2.  
  3. * Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C":
  4.  
  5. */
  6.  
  7. void strreverse(char* begin, char* end) {
  8.  
  9. char aux;
  10.  
  11. while(end>begin)
  12.  
  13. aux=*end, *end--=*begin, *begin++=aux;
  14.  
  15. }
  16.  
  17. void itoa(int value, char* str, int base) {
  18.  
  19. static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  20.  
  21. char* wstr=str;
  22.  
  23. int sign;
  24.  
  25. // Validate base
  26.  
  27. if (base< || base>){ *wstr='\0'; return; }
  28.  
  29. // Take care of sign
  30.  
  31. if ((sign=value) < ) value = -value;
  32.  
  33. // Conversion. Number is reversed.
  34.  
  35. do *wstr++ = num[value%base]; while(value/=base);
  36.  
  37. if(sign<) *wstr++='-';
  38.  
  39. *wstr='\0';
  40.  
  41. // Reverse string
  42.  
  43. strreverse(str,wstr-);
  44.  
  45. }
  46.  
  47. /**
  48.  
  49. * Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C"
  50.  
  51. * with slight modification to optimize for specific architecture:
  52.  
  53. */
  54.  
  55. void strreverse(char* begin, char* end) {
  56.  
  57. char aux;
  58.  
  59. while(end>begin)
  60.  
  61. aux=*end, *end--=*begin, *begin++=aux;
  62.  
  63. }
  64.  
  65. void itoa(int value, char* str, int base) {
  66.  
  67. static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  68.  
  69. char* wstr=str;
  70.  
  71. int sign;
  72.  
  73. div_t res;
  74.  
  75. // Validate base
  76.  
  77. if (base< || base>){ *wstr='\0'; return; }
  78.  
  79. // Take care of sign
  80.  
  81. if ((sign=value) < ) value = -value;
  82.  
  83. // Conversion. Number is reversed.
  84.  
  85. do {
  86.  
  87. res = div(value,base);
  88.  
  89. *wstr++ = num[res.rem];
  90.  
  91. }while(value=res.quot);
  92.  
  93. if(sign<) *wstr++='-';
  94.  
  95. *wstr='\0';
  96.  
  97. // Reverse string
  98.  
  99. strreverse(str,wstr-);
  100.  
  101. }

c语言实现atoi和itoa函数。的更多相关文章

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

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

  2. atoi()和itoa()函数详解以及C语言实现

    atoi()函数 atoi()原型:  int atoi(const char *str ); 函数功能:把字符串转换成整型数. 参数str:要进行转换的字符串 返回值:每个函数返回 int 值,此值 ...

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

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

  4. atoi和itoa函数的实现方法

    atoi的实现: #include<iostream> using namespace std; int atio1(char *s) { int sign=1,num=0; if(*s= ...

  5. C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...

  6. C语言itoa()函数和atoi()函数详解(整数转字符)

    http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...

  7. C语言itoa函数和atoi 函数

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h>  ...

  8. [置顶] C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...

  9. 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...

随机推荐

  1. sign a third-party dll which don't have a strong name

    Problem: Assembly generation failed -- Referenced assembly '' does not have a strong name Cause: thi ...

  2. 发送Email并添加附件

    1. 添加命名空间 using System.Net.Mail; using System.Net; 2. The HTML MarpUp <html xmlns="http://ww ...

  3. 法方总经理用的笔记本电脑&一体机拆开图。

    键盘上有三个字符,

  4. MySQL 5.7.14 安装

    http://www.cnblogs.com/zcGu/articles/5740936.html 因笔者个人需要需要在本机安装Mysql,先将安装过程记录如下,希望对他人有所参考. 一, 1, 进入 ...

  5. 《JavaScript权威指南》拾遗(下)

    一.类和原型         1.在JavaScript中,类的实现是基于原型继承机制的,如果两个实例都是从同一个原型对象中继承了属性,则它们是同一个类的实例.         2.原型对象是类的唯一 ...

  6. Oracle 如何写出高效的 SQL

    转自:Oracle 如何写出高效的 SQL 要想写出高效的SQL 语句需要掌握一些基本原则,如果你违反了这些原则,一般情况下SQL 的性能将会很差. 1. 减少数据库访问次数连接数据库是非常耗时的,虽 ...

  7. Android 常用开源代码整理

    1.AndroidAnnotations一个强大的android开源注解框架, 基本上可以注入任何类型, 比一般的所谓的注入框架要快, 因为他是通过生成一个子类来实现的绑定.具体查看文档. 2.and ...

  8. Ubuntu中文输入法

    这里是Ubuntu12.04,刚把系统语言设成英文,发现输入法没有了. 看看下面是如何找回来的吧. Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等 ...

  9. 转:FileReader详解与实例---读取并显示图像文件

    ~~~针对需要读取本地图像,并立即显示在浏览器的情况,由于chrome firefox出于安全限制,input file并不返回文件的真实路径,经测试IE6/7/8都会返回真实路径,所以chrome, ...

  10. Qt+SQLite数据加密的一种思路(内存数据库)

    了解Qt是比较早的,可是一直没有耐心去做一个练习.近期花了差不多两周时间做了次Qt开发练习,基本完成了Qt的入门,逃脱微软平台又迈出了几小步.Qt虽然是C++的,但开发应用是比较方便的,我觉得它在界面 ...