c語言整型轉字元型
1. 如何用c語言把整形轉換成字元型
使用itoa函數。
原型:extern char *itoa(int i);
參考代碼:
#include<stdio.h>
#include<stdlib.h>
intmain()
{
inta=125;
charb[50];
printf("%s ",itoa(a,b,10));//把10進制的125轉成字元並輸出。
return0;
}
/*
(1)c語言整型轉字元型擴展閱讀:
注意事項
itoa() 函數有3個參數:第一個參數是要轉換的數字,第二個參數是要寫入轉換結果的目標字元串,第三個參數是轉移數字時所用的基數(進制)。在上例中,轉換基數為10,就意味著以10為轉換進制。10:十進制;2:二進制。
itoa 並不是一個標準的C函數,它是Windows特有的,如果要寫跨平台的程序,請用sprintf。
標准庫中有sprintf,功能比這個更強,用法跟printf類似:
2. 高手請進!如何把整形數據轉換為字元串(C語言)
功 能:把一整數轉換為字元串
用 法:char *itoa(int value, char *string, int radix);
詳細解釋:itoa是英文integer to array(將int整型數轉化為一個字元串,並將值保存在數組string中)的縮寫.
參數:
value: 待轉化的整數。
radix: 是基數的意思,即先將value轉化為radix進制的數,范圍介於2-36,比如10表示10進制,16表示16進制。
* string: 保存轉換後得到的字元串。
返回值:
char * : 指向生成的字元串, 同*string。
備注:該函數的頭文件是"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;
}
注釋:編譯系統:VC++6.0,TC不支持。
我們可以這樣構造itoa()
char* itoa(int i)
{
char *a=malloc(42); /* Enough for a 128 bit integer */
if (a) sprintf(a,"%d",i);
return a;
}
實現itoa函數的源代碼
char *my_itoa(int num,char *str,int radix){
const char table[]="";
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(num<0){ //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(start<ptr){
char temp = *start;
*start = *ptr;
*ptr = temp;
start++;
ptr--;
}
return str;
}
3. C語言如何將一個整型數值轉化為字元
int iValue;//整型數
char sz[10];//字元串
sprintf(sz, "%d", iValue);//這句需要頭文件#include <stdio.h>
/*或者*/
itoa(iValue, sz, 10); //這句需要頭文件#include <ctype.h>
sprintf類似於printf,printf比sprintf少第一個參數,就是直接在輸出界面輸出相應的東西,而sprintf就是將你要輸出的東西按相應格式存放到第一個參數的字元串中。
itoa是直接將整型數轉化成字元串
4. C語言中怎麼把Int型轉成字元型
要先弄清楚到底是將整型轉換成字元還是一個字元串,如果把整型變數轉為以該變數作為ascii碼的字元,可以直接付值,當然這個數值要小於256,如果是轉換成一個字元串,那就如上說講的用sprintf或者itoa函數來實現吧
5. 用c語言怎麼將整數轉換成字元串
1.int/float to string/array:
C語言提供了幾個標准庫函數,可以將任意類型(整型、長整型、浮點型等)的數字轉換為字元串,下面列舉了各函數的方法及其說明。
● itoa():將整型值轉換為字元串。
● ltoa():將長整型值轉換為字元串。
● ultoa():將無符號長整型值轉換為字元串。
● gcvt():將浮點型數轉換為字元串,取四捨五入。
● ecvt():將雙精度浮點型值轉換為字元串,轉換結果中不包含十進制小數點。
● fcvt():指定位數為轉換精度,其餘同ecvt()。
除此外,還可以使用sprintf系列函數把數字轉換成字元串,其比itoa()系列函數運行速度慢
2. string/array to int/float
C/C++語言提供了幾個標准庫函數,可以將字元串轉換為任意類型(整型、長整型、浮點型等)。
● atof():將字元串轉換為雙精度浮點型值。
● atoi():將字元串轉換為整型值。
● atol():將字元串轉換為長整型值。
● strtod():將字元串轉換為雙精度浮點型值,並報告不能被轉換的所有剩餘數字。
● strtol():將字元串轉換為長整值,並報告不能被轉換的所有剩餘數字。
● strtoul():將字元串轉換為無符號長整型值,並報告不能被轉換的所有剩餘數字。
C語言實現:
#include
#include
void itoa (int n,char s[]);
//atoi 函數:將s轉換為整形數
int main(void )
{
int n;
char s[100];
printf("Input n: ");
scanf("%d",&n);
printf("the string : ");
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]='