----------------------------------------------------------------------------------------------------
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转 换为字符串的一个例子:
# include <stdio.h>
# include <stdlib.h>
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);
printf("The number ’num’ is %d and the string ’str’ is %s. /n" ,
num, str);
}
itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制...
itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:
char str[255];
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。
下列函数可以将整数转换为字符串:
----------------------------------------------------------
函数名 作 用
----------------------------------------------------------
itoa() 将整型值转换为字符串
itoa() 将长整型值转换为字符串
ultoa() 将无符号长整型值转换为字符串
一 atoi 把字符串转换成整型数
例程序:
#include <ctype.h>
#include <stdio.h>
int atoi (char s[]);
int main(void )
{
char s[100];
gets(s);
printf("integer=%d/n",atoi(s));
return 0;
}
int atoi (char s[])
{
int i,n,sign;
for(i=0;isspace(s[i]);i++)//跳过空白符
;
sign=(s[i]==’-’)?-1:1;
if(s[i]==’+’||s[i]==’ -’)//跳过符号
i++;
for(n=0;isdigit(s[i]);i++)
n=10*n+(s[i]-’0’);//将数字字符转换成整形数字
return sign *n;
}
二 itoa 把一整数转换为字符串
例程序:
#include <ctype.h>
#include <stdio.h>
void itoa (int n,char s[]);
//atoi 函数:将s转换为整形数
int main(void )
{
int n;
char s[100];
printf("Input n:/n");
scanf("%d",&n);
printf("the string : /n");
itoa (n,s);
return 0;
}
void itoa (int n,char s[])
{
int i,j,sign;
if((sign=n)<0)//记录符号
n=-n;//使n成为正数 i=0;
do{
s[i++]=n%10+’0’;//取下一个数字
}while ((n/=10)>0);//删除该数字
if(sign<0)
s[i++]=’-’;
s[i]=’/0’;
for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出
printf("%c",s[j]);
}
------------------------------------------------------------------------------------
自己写的atoi函数----(注意:自己定义的atoi函数和库的atoi函数一样的时候,抛出异常时会引起异常退出,个人认为是异常没有不知道被那个函数抛出,所以coredump)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <iostream>
#include <string>
#include <exception>
using namespace std;
const unsigned int SIGN_BIT = 0x1 << 31;
bool isDigit(const char ch)
{
if (ch <= '9' && ch >= '0')
{
return true;
}
return false;
}
int atoi_i(const char *str)
{
assert(str != NULL);
while (' ' == *str){ str++; }
int result = 0;
bool signFlag = false;
if ('+' == *str)
{
if (false == isDigit(*++str)) throw "input format error!";
}
else if ('-' == *str)
{
if (false == isDigit(*++str)) throw "input format error!";
signFlag = true;
}
else if (*str > '9' || *str < '0')
{
throw "input format error!";
}
do
{
result = result * 10 + *str++ - '0';
if ((result & SIGN_BIT) != 0)
{
throw "overflow error!";
}
}
while (isDigit(*str));
if (true == signFlag)
{
result = -result;
}
return result;
}
int main(int argc, char *argv[])
{
char input[1024];
while (1)
{
try
{
cout << "Input Array:";
cin >> input;
printf("exchange:%d/n", atoi_i(input));
}
catch (const char *p)
{
cout <<"Error Info:" << p << endl;
}
catch ( ... )
{
cout << "test" << endl;
}
}
return 0;
}
-----------------------------------------------------------------------------------
itoa函数(自己)
#define SWAP(X,Y) (X) = (X) ^ (Y); (Y) = (X) ^ (Y); (X) = (X) ^ (Y)
char *itoa_i(int src, char *a)
{
bool signFlag = false;
if (src < 0)
{
src = -src;
signFlag = true;
}
int k = 0;
do
{
a[k++] = src % 10 + '0';
}
while((src /= 10) > 0);
if (true == signFlag)
{
a[k++] = '-';
}
a[k] = '/0';
for (int i = 0, j = k - 1; i < j; ++i, --j)
{
SWAP(*(a+i),*(a+j));
}
return a;
}