深入理解atoi()與itoa()函數(shù)的用法
更新時間:2013年05月24日 11:44:45 作者:
本篇文章是對atoi()與itoa()函數(shù)的用法進行了詳細的分析介紹,需要的朋友參考下
itoa()函數(shù)的原型為: char *itoa( int value, char *string,int radix);
itoa()函數(shù)有3個參數(shù):第一個參數(shù)是要轉(zhuǎn)換的數(shù)字,第二個參數(shù)是要寫入轉(zhuǎn)換結(jié)果的目標字符串,第三個參數(shù)是轉(zhuǎn)換數(shù)字時所用的基數(shù)。在例中,轉(zhuǎn)換基數(shù)為10。10:十進制;2:二進制...
itoa并不是一個標準的C函數(shù),它是Windows特有的,如果要寫跨平臺的程序,請用sprintf。
是Windows平臺下擴展的,標準庫中有sprintf,功能比這個更強,用法跟printf類似:
char str[255];
sprintf(str, "%x", 100); //將100轉(zhuǎn)為16進制表示的字符串。
下面是一個十進制轉(zhuǎn)八進制的方法:
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int num = 10;
char str[100];
itoa(num, str, 8); //將整數(shù)10轉(zhuǎn)換為八進制保存在str字符數(shù)組中
printf("%s\n", str);
system("pause");
return 0;
}
下面是一個十進制轉(zhuǎn)二進制的方法:
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int num = 15;
char str[100];
int n = atoi(itoa(num, str, 2)); //先把num轉(zhuǎn)換為二進制的字符串,再把該字符串轉(zhuǎn)換為整數(shù)
printf("%d\n",n);
system("pause");
return 0;
}
itoa()函數(shù)的擴展:
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 );
程序代碼如下:
#include "stdio.h"
#include "stdlib.h"
int 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 );
system("pause");
return 0;
}
itoa()函數(shù)有3個參數(shù):第一個參數(shù)是要轉(zhuǎn)換的數(shù)字,第二個參數(shù)是要寫入轉(zhuǎn)換結(jié)果的目標字符串,第三個參數(shù)是轉(zhuǎn)換數(shù)字時所用的基數(shù)。在例中,轉(zhuǎn)換基數(shù)為10。10:十進制;2:二進制...
itoa并不是一個標準的C函數(shù),它是Windows特有的,如果要寫跨平臺的程序,請用sprintf。
是Windows平臺下擴展的,標準庫中有sprintf,功能比這個更強,用法跟printf類似:
char str[255];
sprintf(str, "%x", 100); //將100轉(zhuǎn)為16進制表示的字符串。
下面是一個十進制轉(zhuǎn)八進制的方法:
復制代碼 代碼如下:
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int num = 10;
char str[100];
itoa(num, str, 8); //將整數(shù)10轉(zhuǎn)換為八進制保存在str字符數(shù)組中
printf("%s\n", str);
system("pause");
return 0;
}
下面是一個十進制轉(zhuǎn)二進制的方法:
復制代碼 代碼如下:
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int num = 15;
char str[100];
int n = atoi(itoa(num, str, 2)); //先把num轉(zhuǎn)換為二進制的字符串,再把該字符串轉(zhuǎn)換為整數(shù)
printf("%d\n",n);
system("pause");
return 0;
}
itoa()函數(shù)的擴展:
復制代碼 代碼如下:
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 );
程序代碼如下:
復制代碼 代碼如下:
#include "stdio.h"
#include "stdlib.h"
int 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 );
system("pause");
return 0;
}
相關文章
C語言scandir函數(shù)獲取文件夾內(nèi)容的實現(xiàn)
scandir?函數(shù)用于列舉指定目錄下的文件列表,本文主要介紹了C語言scandir函數(shù)獲取文件夾內(nèi)容的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03C語言中const,volatile,restrict的用法總結(jié)
以下是對C語言中const,volatile,restrict的用法進行了詳細的總結(jié)介紹,需要的朋友可以過來參考下2013-10-10