原文地址:http://blog.csdn.net/ouyangzp/article/details/2570376
itoa函数及atoi函数
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]);
}
是int 转string类型的一个函数
msdn上是这么写的
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
Convert an integer to a string.
char *_itoa( int value, char *string, int radix );
char *_i64toa( __int64 value, char *string, int radix );
char * _ui64toa( unsigned _int64 value, char *string, int radix );
wchar_t * _itow( int value, wchar_t *string, int radix );
wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );
wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );
Routine Required Header Compatibility
_itoa <stdlib.h> Win 95, Win NT
_i64toa <stdlib.h> Win 95, Win NT
_ui64toa <stdlib.h> Win 95, Win NT
_itow <stdlib.h> Win 95, Win NT
_i64tow <stdlib.h> Win 95, Win NT
_ui64tow <stdlib.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Each of these functions returns a pointer to string. There is no error return.
Parameters
value
Number to be converted
string
String result
radix
Base of value; must be in the range 2 – 36
Remarks
The _itoa, _i64toa, and _ui64toa function convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 bytes) in string. If radix equals 10 and value is negative, the first character of the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa respectively.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_itot _itoa _itoa _itow
Example
/* ITOA.C: This program converts integers of various
* sizes to strings in various radixes.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s/n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s/n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s/n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s/n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s/n", ul,
buffer );
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
Data Conversion Routines
See Also _ltoa, _ultoa
分享到:
相关推荐
itoa函数及atoi函数的区别,C++,MFC
学习自己实现数值转字符itoa函数、字符串转数值atoi函数和求字符串长度strlen函数
字符串和整形互相转换函数atoi,itoa函数原型认识两个函数以及用法
字符串和整形互相转换函数atoi,itoa函数原型 认识两个函数以及用法
本文介绍了,atoi和itoa函数的实现方法,需要的朋友可以参考一下
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。
本篇文章是对atoi()与itoa()函数的用法进行了详细的分析介绍,需要的朋友参考下
本篇文章是对atoi()与itoa()函数的内部实现方法进行了详细的分析介绍,需要的朋友参考下
1.7 itoa函数和atoi函数 1.8 strcmp函数实现 1.9 strcpy函数实现 1.10 memcpy函数实现 1.11 memcpy和memmove函数的实现 1.12 strcat函数实现 1.13 使用库函数atoi,将char *→int 1.14 使用库函数itoa,将int→...
字符串是Auto Lisp的基本数据之一,它常用于磁盘文件名,标识符的打印名等。Auto Lisp语言和其它高级语言一样,提供了一些字符串... ·ITOA ·ATOI ·ATOF ·RTOS ·ANGTOS ·STRCAT ·SUBSTR ·STRCASE ·READ
自己对源代码做的一些整理,包括:atoi itoa strcpy strcat strlen memcpy memset等等常用的字符串处理函数,提供源代码实现。
C语言常用库函数 主要介绍了strlen strcmp strcpy strcat strstr strchi atoi itoa等函数的实现方法。
* 常见的字符串函数实现: * strlen * strcpy strncpy * strcmp strncmp * strcat strncat * strstr * * 内存操作: * memset、memcmp、memcpy、memmove * * 字符串和数组转换: * atoi itoa *
减少库的使用,解决那些需要小代码量,但苦恼于没有简易的字符串处理函数的郁闷 char *itoa_private(int val, char *buf, unsigned radix);//整数转字符串 int my_isdigit(int ch);//判断字符是否为数字 long long ...
n,err := strconv.Atoi(123) if err != nil { fmt.Println(err) }else{ fmt.Println(n) } } 结果 123 整数转字符串 str := strconv.Itoa(123) fmt.Println(str) 结果 “123” 字符串转[]byte var bytes...
可我找不到任何方法来声明这样的函数——感觉我需要一个返回指针的函数,返回的指针指向的又是返回指针的函数……,如此往复,以至无穷。 12 数组大小 13 1.23 能否声明和传入数组大小一致的局部数组,或者由...
可我找不到任何方法来声明这样的函数——感觉我需要一个返回指针的函数,返回的指针指向的又是返回指针的函数……,如此往复,以至无穷。 数组大小 1.23 能否声明和传入数组大小一致的局部数组,或者由其他参数...
o 6.6 如果 NULL 定义成 #define NULL ((char *)0) 难道不就可以向函数传入不加转换的 NULL 了吗? o 6.7 如果 NULL 和 0 作为空指针常数是等价的, 那我到底该用哪一个呢? o 6.8 但是如果 NULL 的值改变了, 比如...