Ⅰ 在c語言中怎麼把一個整數轉化為字元串
你可以使用庫函數 itoa()
# include
# include
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);//把int 類型的num 轉換成 10 進制的字元串類型
printf("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
}
Ⅱ C語言 將一個整數轉換成一個字元串
atoi: 把字元串轉換成整型數
itoa:把整數轉換為字元串
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int number = 12345;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
哇塞要上面那麼復雜嗎
Ⅲ C語言 將輸入整數轉換成字元串輸出
在這里先假設n的值為123。
*s = '0'+i%10; i%10相當於取i取的個位數,即數字3. 字元0 加上 整數3就是字元3.(這個要弄明白哦)。所以字元s的字為3.
itoa(i/10,s-1); i/10即 123/10 = 12. 迭代函數, 字元指針s後退一位,s-1 = 2;
同理推出 s-2 = 1.itoa(n,str+5); str的長度是7。
即:str[0],str[1],str[2],str[3],str[4],str[5],str[6].
str + 5 即把地址指向 str[5].
看函數itoa(),
n為 123 時
str[0] = '';
str[1] = '';
str[2] = '';
str[3] = '1';
str[4] = '2';
str[5] = '3';
str[6] = '';
Ⅳ 高手請進!如何把整形數據轉換為字元串(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;
}
Ⅳ 用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]='