C語言 itoa函式及atoi函式

helloxchen發表於2010-12-09

toa函式及atoi函式
2007-05-11 13:52
C語言提供了幾個標準庫函式,可以將任意型別(整型、長整型、浮點型等)的數字轉換為字串。以下是用itoa()函式將整數轉 換為字串的一個例子:

# include
# include

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
#include
int atoi (char s[]);

int main(void )
{
char s[100];
gets(s);
printf("integer=%dn",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
#include
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]='';
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 Win 95, Win NT
_i64toa Win 95, Win NT
_ui64toa Win 95, Win NT
_itow Win 95, Win NT
_i64tow Win 95, Win NT
_ui64tow 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
#include

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): %sn", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%sn", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %sn", i, buffer );

_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%sn", l,
buffer );

_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%sn", 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

http://blog.csdn.net/sayigood/archive/2010/01/03/5119884.aspx

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24790158/viewspace-1042846/,如需轉載,請註明出處,否則將追究法律責任。

相關文章