整理atoi itoa的实现

一、atoi

原型:

int atoi(const char *nptr);

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

  1 #include<stdlib.h>
  2 #include<stdio.h>
  3 int main(void)
  4 {
  5 #if 0
  6     float n;
  7     char*str="12345.67cd";//12345.000000
  8 //  char*str="-12345.67cd";//-12345.000000
  9 //  char*str="ab12345.67cd";//0.000000
 10     n=atoi(str);
 11     printf("string=%s\ninteger=%f\n",str,n);
 12 #else
 13
 14     char a[]="-110";
 15     char b[]="123";
 16     int  c;
 17     c = atoi(a) + atoi(b);
 18     printf("c=%d\n",c);//13
 19 #endif
 20     return 0;
 21 }

 

2、itoa

char *itoa( int value, char *string,int radix);
原型说明:
value欲转换的数据。
string:目标字符串的地址。
radix:转换后的进制数,可以是10进制、16进制等。
 
程序实例:
 
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
    int number = 12345;
    char string[32];
    itoa(number, string, 10);
    printf("integer = %d string = %s\n", number, string);
    return 0;
}
itoa 并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
标准库中有sprintf,功能比这个更强,用法跟printf类似:
char str[255];
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。
 摘抄itoa实现:
 
1、
void itoa(int n,char s[])
{
int i,j,k,sign;
char tmp;
if((sign=n)<0)
       n=-n;
        i=0;
do{
         s[i++]=n%10+'0';
  }
while ((n/=10)>0);
        if(sign<0)
                 s[i++]='-';
s[i]='\0';
//printf("debug int n: %d\n",n); 
//printf("debug char s: %s\n",s);
/*
for debug input int i and output char s[];
*/
for(j=i-1,k=0;j>((i-1)/2);j--,k++)
 {
       tmp=s[k];
      s[k]=s[j];
      s[j]=tmp;
 }
 //printf("debug str: %s\n",s);
2、
转一个高效方法
C/C++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 char   *myitoa(   int   value,   char   *str,   int   radix   )   
  {   
  static   char   szMap[]   =   {   
  '0',   '1',   '2',   '3',   '4',   '5',   
  '6',   '7',   '8',   '9',   'a',   'b',   
  'c',   'd',   'e',   'f',   'g',   'h',   
  'i',   'j',   'k',   'l',   'm',   'n',   
  'o',   'p',   'q',   'r',   's',   't',   
  'u',   'v',   'w',   'x',   'y',   'z'   
  }; //   字符映射表   
  int   nCount   =   -1,   nIndex;   
  char   *pStr   =   str,   nTemp;   
  if   (   radix   >=   2   &&   radix   <=   36   )   
  {   //   限制radix必须在2到36之间   
  if   (   value   <   0   &&   radix   ==   10   )   
  {   //   如果是负数就在首位添加负号,并将字符串前移   
  *pStr++   =   '-';   
  value   =   -value;   //转为正数,   
  }   
  unsigned   int   nValue   =   *(unsigned*)&value;   
  do   //   循环转换每一个数字,直到结束   
  pStr[   ++nCount   ]   =   szMap[   nValue   %   radix   ];   
  nValue   /=   radix;   
  }   while(   nValue   >   0   );   //   转换结束后字符串是翻的   
  nIndex   =   (   nCount   +   1   )   /   2;   //   计算出一半的长度   
  while(   nIndex--   >   0   )   {   //   将字符串的字符序翻转   
  nTemp   =   pStr[   nIndex   ];   
  pStr[   nIndex   ]   =   pStr[   nCount   -   nIndex   ];   
  pStr[   nCount   -   nIndex   ]   =   nTemp;   
  }   
  }   
  pStr[   nCount   +   1   ]   =   '\0';   //   置结束符   
  return   str;   
  }   
   
3、转一个看看
C/C++ code
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***
*atox.c - atoi and atol conversion
*
* Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Converts a character string into an int or long.
*
*******************************************************************************/
 
#include <cruntime.h>
#include <stdlib.h>
#include <ctype.h>
 
/***
*long atol(char *nptr) - Convert string to long
*
*Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected.
*
*Entry:
* nptr = ptr to string to convert
*
*Exit:
* return long int value of the string
*
*Exceptions:
* None - overflow is not detected.
*
*******************************************************************************/
 
long __cdecl atol(
const char *nptr
)
{
int c; /* current char */
long total; /* current total */
int sign; /* if ''-'', then negative, otherwise positive */
 
/* skip whitespace */
while isspace((int)(unsigned char)*nptr) )
++nptr;
 
c = (int)(unsigned char)*nptr++;
sign = c; /* save sign indication */
if (c == ''-'' || c == ''+'')
c = (int)(unsigned char)*nptr++; /* skip sign */
 
total = 0;
 
while (isdigit(c)) {
total = 10 * total + (c - ''0''); /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}
 
if (sign == ''-'')
return -total;
else
return total; /* return result, negated if necessary */
}
 
 
/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected. Because of this, we can just use
* atol().
*
*Entry:
* nptr = ptr to string to convert
*
*Exit:
* return int value of the string
*
*Exceptions:
* None - overflow is not detected.
*
*******************************************************************************/
 
int __cdecl atoi(
const char *nptr
)
{
return (int)atol(nptr);
}
 
#ifndef _NO_INT64
 
__int64 __cdecl _atoi64(
const char *nptr
)
{
int c; /* current char */
__int64 total; /* current total */
int sign; /* if ''-'', then negative, otherwise positive */
 
/* skip whitespace */
while isspace((int)(unsigned char)*nptr) )
++nptr;
 
c = (int)(unsigned char)*nptr++;
sign = c; /* save sign indication */
if (c == ''-'' || c == ''+'')
c = (int)(unsigned char)*nptr++; /* skip sign */
 
total = 0;
 
while (isdigit(c)) {
total = 10 * total + (c - ''0''); /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}
 
if (sign == ''-'')
return -total;
else
return total; /* return result, negated if necessary */
}
 
#endif /* _NO_INT64 */
 
 
#include <msvcrt/errno.h>
#include <msvcrt/stdlib.h>
#include <msvcrt/internal/file.h>
char* _itoa(int value, char* string, int radix)
{
char tmp[33];
char* tp = tmp;
int i;
unsigned v;
int sign;
char* sp;
 
if (radix > 36 || radix <= 1)
{
__set_errno(EDOM);
return 0;
}
 
sign = (radix == 10 && value < 0);
if (sign)
v = -value;
else
v = (unsigned)value;
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+''0'';
else
*tp++ = i + ''a'' - 10;
}
 
if (string == 0)
string = (char*)malloc((tp-tmp)+sign+1);
sp = string;
 
if (sign)
*sp++ = ''-'';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}
 
4、
1 //n是要被转换的十进制数,bit是要被转换的进制数
2 #include <iostream>
3 #include <string>
4 using namespace std;
5
6 char * Ten_to_other(int num,char* str, int radix)
7 {
8     const char a[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
9     char *ptr=str;
10     bool negtive = false;
11     if(num==0)
12     {
13         *ptr++=0;
14         *ptr='\0';
15         return NULL;//直接就退出,不走后面的情况了,不跟后面共用一个return语句。
16     }
17     if(num<0)
18     {
19         num *= -1;
20         *ptr++ ='-';
21         negtive = true;
22     }
23     while(num) //不能处理num=0的情况
24     {
25         *ptr++=a[num%radix];
26         num /= radix;
27     }
28     *ptr='\0';//此时ptr已经指向字符串结尾,而str仍旧指向字符串开始。
29     ptr--;//让ptr指向字符串内容!!
30     char *start=(negtive)? str+1:str;//注意此处不能写str++,否则,str不指向保存值的首地址了
31     while(start<ptr)
32     {
33         char tmp=*ptr;
34         *ptr=*start;
35         *start=tmp;
36         ptr--;
37         start++;
38         /*char tmp=*ptr--;//这样写ptr,str都加了那么多遍,肯定错了啊!!!
39         *ptr--=*str++;
40         *str++=tmp;*/
41     }
42     /*int len=strlen(str);
43     int i = (negtive)? 1:0;
44     for(i; i<len/2; ++i) //当为负数时候,str字符串最后的字符不能用len-1-i了,因为此时i=1而不是0
45     {
46         char tmp = str[i];
47         str[i]=str[len-1-i];
48         str[len-1-i]=tmp;
49     } */
50     return NULL;
51 }
52 int main()
53 {
54     char a[12]={0};
55     Ten_to_other(-82,a,6);
56     cout<<a<<endl;
57     system("pause");
58     return 0;
59 }
 
 
 
 
 
 
 
posted on 2014-07-06 19:37  flying06  阅读(271)  评论(0编辑  收藏  举报