一、將數字轉換爲字符串
C語言提供了幾個標準庫函數,可以將任意類型(整型、長整型、浮點型等)的數字轉換爲字符串。以下是用itoa()函數將整數轉換爲字符串的一個例子:
# include <stdio. h>
# include <stdlib. h>
void main (void);
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。
下列函數可以將整數轉換爲字符串:
----------------------------------------------------------
函數名 作 用
----------------------------------------------------------
itoa() 將整型值轉換爲字符串
itoa() 將長整型值轉換爲字符串
ultoa() 將無符號長整型值轉換爲字符串
----------------------------------------------------------
請注意,上述函數與ANSI標準是不兼容的。能將整數轉換爲字符串而且與ANSI標準兼容的方法是使用sprintf()函數,請看下例:
#include<stdio.h>
# include <stdlib. h>
void main (void);
void main (void)
{
int num = 100;
char str[25];
sprintf(str, " %d" , num);
printf ("The number 'num' is %d and the string 'str' is %s. /n" ,
num, str);
}
在將浮點型數字轉換爲字符串時,需要使用另外一組函數。以下是用fcvt()函數將浮點型值轉換爲字符串的一個例子:
# include <stdio. h>
# include <stdlib. h>
void main (void);
void main (void)
{
double num = 12345.678;
char * sir;
int dec_pl, sign, ndigits = 3; /* Keep 3 digits of precision. * /
str = fcvt(num, ndigits, &dec-pl, &sign); /* Convert the float
to a string. * /
printf("Original number; %f/n" , num) ; /* Print the original
floating-point
value. * /
printf ("Converted string; %s/n",str); /* Print the converted
string's value. * /
printf ("Decimal place: %d/n" , dec-pi) ; /* Print the location of
the decimal point. * /
printf ("Sign: %d/n" , sign) ; /* Print the sign.
0 = positive,
1 = negative. * /
}
fcvt()函數和itoa()函數有數大的差別。fcvt()函數有4個參數:第一個參數是要轉換的浮點型值;第二個參數是轉換結果中十進制小數點右側的位數;第三個參數是指向一個整數的指針,該整數用來返回轉換結果中十進制小數點的位置;第四個參數也是指向一個整數的指針,該整數用來返回轉換結果的符號(0對應於正值,1對應於負值)。
需要注意的是,fcvt()函數的轉換結果中並不真正包含十進制小數點,爲此,fcvt()函數返回在轉換結果中十進制小數點應該佔據的位置。在上例中,整型變量dec_pl的結果值爲5,因爲在轉換結果中十進制小數點應該位於第5位後面。如果你要求轉換結果中包含十進制小數點,你可以使用gcvt()函數(見下表)。
下列函數可以將浮點型值轉換爲字符串:
-------------------------------------------------------------------------
函數名 作 用
-------------------------------------------------------------------------
ecvt() 將雙精度浮點型值轉換爲字符串,轉換結果中不包含十進制小數點
fcvt() 以指定位數爲轉換精度,餘同ecvt()
gcvt() 將雙精度浮點型值轉換爲字符串,轉換結果中包含十進制小數點
另外怎樣在QT中將整數型轉換成字符串?
C 中的itoa沒包含在QT中.謝謝了!
QString str = QString("%1").arg(iYourInteger);
# include <stdio. h>
# include <stdlib. h>
void main (void) ;
void main (void)
{
int num;
char * str = "100";
num = atoi(str);
printf("The string 'str' is %s and the number 'num' is %d. /n",
str, num);
}
atoi()函數只有一個參數,即要轉換爲數字的字符串。atoi()函數的返回值就是轉換所得的整型值。
下列函數可以將字符串轉換爲數字:
------------------------------------------------------------------------
函數名 作 用
------------------------------------------------------------------------
atof() 將字符串轉換爲雙精度浮點型值
atoi() 將字符串轉換爲整型值
atol() 將字符串轉換爲長整型值
strtod() 將字符串轉換爲雙精度浮點型值,並報告不能被轉換的所有剩餘數字
strtol() 將字符串轉換爲長整值,並報告不能被轉換的所有剩餘數字
strtoul() 將字符串轉換爲無符號長整型值,並報告不能被轉換的所有剩餘數字
------------------------------------------------------------------------
將字符串轉換爲數字時可能會導致溢出,如果你使用的是strtoul()這樣的函數,你就能檢查這種溢出錯誤。請看下例:
# include <stdio. h>
# include <stdlib. h>
# include <limits. h>
void main(void);
void main (void)
{
char* str = "1234567891011121314151617181920" ;
unsigned long num;
char * leftover;
num = strtoul(str, &leftover, 10);
printf("Original string: %s/n",str);
printf("Converted number: %1u/n" , num);
printf("Leftover characters: %s/n" , leftover);
}
在上例中,要轉換的字符串太長,超出了無符號長整型值的取值範圍,因此,strtoul()函數將返回ULONG_MAX(4294967295),並使。char leftover指向字符串中導致溢出的那部分字符;同時,strtoul()函數還將全局變量errno賦值爲ERANGE,以通知函數的調用者發生了溢出錯誤。函數strtod()和strtol()處理溢出錯誤的方式和函數strtoul()完全相同,你可以從編譯程序文檔中進一步瞭解這三個函數的有關細節。