itoa实现类型转换
来源:互联网 发布:64码高清网络电视vlc 编辑:程序博客网 时间:2024/05/07 01:13
itoa函数及atoi函数:语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符
串。以下是用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]);
}
ps:注意,有些平台不支持itoa,其并非标准模板~~~慎用
- itoa实现类型转换
- itoa与sprintf实现类型转换
- itoa 转换
- C++ 类型转换 atoi atol atof itoa ftoa <stdlib.h>
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
- itoa实现
- itoa 实现
- 利用itoa实现10进制数到2进制数的转换
- itoa函数各进制转换
- Montavista linux DevRocket集成开发环境
- c++的意义。。(个人感觉使用c++可以接触到软件的本质,虽然不能直接开发快速但你可以有自由的感觉,尤其是指针,和直接贴近内存的本质)
- 使用java处理HTTP返回302的问题
- 设计师们是如何建立战略的?
- SVN使用技巧
- itoa实现类型转换
- 想做什么样的首页?只有你想不到的,没有我做不到的
- IIS常见问题及解答以及故障分析
- 裴琳为吸毒事件鞠躬认错狂飚泪
- YUV转RGB函数
- 公主小妹第10集播放(完整版)
- C#实现登录验证码
- 分析下周股市五大猜想
- VC学习一日一练(1)---为菜单项前添加图标