1、一位的整型數(shù)轉(zhuǎn)換為字符。首先,定義一個整型變量,保存轉(zhuǎn)換的一位整型數(shù)值。
安遠(yuǎn)網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,安遠(yuǎn)網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為安遠(yuǎn)1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的安遠(yuǎn)做網(wǎng)站的公司定做!
2、接著,定義一個字符型變量,保存轉(zhuǎn)換為的單個字符。
3、將數(shù)值變量加上48,然后保存在字符變量c中。
4、運行程序,輸入一個一位的數(shù)值后,電腦就會把它轉(zhuǎn)換為相應(yīng)的字符。
5、還可以將轉(zhuǎn)換表達(dá)式中的48,更改為字符0,也能實現(xiàn)相同的效果。
6、多位的整型數(shù)轉(zhuǎn)換為字符,如果整數(shù)有多位,將其轉(zhuǎn)換為字符串,可以使用itoa函數(shù)。在程序中,定義一個字符類型數(shù)組s。
7、使用itoa函數(shù)將數(shù)值轉(zhuǎn)換為相應(yīng)的字符串,保存在數(shù)組s中。
8、運行程序,輸入一個數(shù)值,電腦就會將其轉(zhuǎn)換為相應(yīng)的字符串。
功 能:把一整數(shù)轉(zhuǎn)換為字符串
用 法:char *itoa(int value, char *string, int radix);
詳細(xì)解釋:itoa是英文integer to array(將int整型數(shù)轉(zhuǎn)化為一個字符串,并將值保存在數(shù)組string中)的縮寫.
參數(shù):
value: 待轉(zhuǎn)化的整數(shù)。
radix: 是基數(shù)的意思,即先將value轉(zhuǎn)化為radix進(jìn)制的數(shù),范圍介于2-36,比如10表示10進(jìn)制,16表示16進(jìn)制。
* string: 保存轉(zhuǎn)換后得到的字符串。
返回值:
char * : 指向生成的字符串, 同*string。
備注:該函數(shù)的頭文件是"stdlib.h"
程序例:
#include stdlib.h
#include stdio.h
int main()
{
int number = 123456;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
注釋:編譯系統(tǒng):VC++6.0,TC不支持。
我們可以這樣構(gòu)造itoa()
char* itoa(int i)
{
char *a=malloc(42); /* Enough for a 128 bit integer */
if (a) sprintf(a,"%d",i);
return a;
}
實現(xiàn)itoa函數(shù)的源代碼
char *my_itoa(int num,char *str,int radix){
const char table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *ptr = str;
bool negative = false;
if(num == 0){ //num=0
*ptr++='0';
*ptr='\0'; // don`t forget the end of the string is '\0'!!!!!!!!!
return str;
}
if(num0){ //if num is negative ,the add '-'and change num to positive
*ptr++='-';
num*=-1;
negative = true;
}
while(num){
*ptr++ = table[num%radix];
num/=radix;
}
*ptr = '\0'; //if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char *start =(negative?str+1:str); //now start points the head of the string
ptr--; //now prt points the end of the string
while(startptr){
char temp = *start;
*start = *ptr;
*ptr = temp;
start++;
ptr--;
}
return str;
}
函數(shù)原形
char
*itoa(int
value,
char
*string,
int
radix)
將整形數(shù)value轉(zhuǎn)換為其等價的字符串
頭文件stdlib.h
Parameters(參數(shù)說明)
value
Number
to
be
converted(將要被轉(zhuǎn)換的值)
string
String
result(轉(zhuǎn)換的結(jié)果)
radix
Base
of
value;
must
be
in
the
range
2
–
36
(轉(zhuǎn)換的基數(shù),取值范圍2-36。例如radix=10表示10進(jìn)制,radix=8表示8進(jìn)制。)
返回值:與string參數(shù)相同,便于函數(shù)的嵌套調(diào)用
例子:(來自MSDN,有刪改)
#i
nclude
#i
nclude
void
main(
void
)
{
char
buffer[20];
int
i
=
3445;
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
);
system("pause");
}
int
iValue;//整型數(shù)
char
sz[10];//字符串
sprintf(sz,
"%d",
iValue);//這句需要頭文件#include
stdio.h
/*或者*/
itoa(iValue,
sz,
10);
//這句需要頭文件#include
ctype.h
sprintf類似于printf,printf比sprintf少第一個參數(shù),就是直接在輸出界面輸出相應(yīng)的東西,而sprintf就是將你要輸出的東西按相應(yīng)格式存放到第一個參數(shù)的字符串中。
itoa是直接將整型數(shù)轉(zhuǎn)化成字符串
使用itoa函數(shù)。
原型:extern char *itoa(int i);
參考代碼:
#includestdio.h
#includestdlib.h
int?main()
{
int?a=125;
char?b[50];?
printf("%s\n",itoa(a,b,10));//把10進(jìn)制的125轉(zhuǎn)成字符并輸出。
return?0;
}
/*
擴展資料:
注意事項
itoa() 函數(shù)有3個參數(shù):第一個參數(shù)是要轉(zhuǎn)換的數(shù)字,第二個參數(shù)是要寫入轉(zhuǎn)換結(jié)果的目標(biāo)字符串,第三個參數(shù)是轉(zhuǎn)移數(shù)字時所用的基數(shù)(進(jìn)制)。在上例中,轉(zhuǎn)換基數(shù)為10,就意味著以10為轉(zhuǎn)換進(jìn)制。10:十進(jìn)制;2:二進(jìn)制。
itoa 并不是一個標(biāo)準(zhǔn)的C函數(shù),它是Windows特有的,如果要寫跨平臺的程序,請用sprintf。
標(biāo)準(zhǔn)庫中有sprintf,功能比這個更強,用法跟printf類似:
參考資料來源:百度百科-itoa函數(shù)
本文標(biāo)題:c語言整形轉(zhuǎn)為字符串函數(shù) c語言 整形轉(zhuǎn)字符串
URL標(biāo)題:http://aaarwkj.com/article28/hhhgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站排名、外貿(mào)建站、標(biāo)簽優(yōu)化、App設(shè)計、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)