韌館-LearnHouse

[C/C++]整數轉字串 不能用itoa ??

神奇了!!為什麼不能用itoa的函數呢??明明已經include stdio.h和stdlib.h

但還是找不到定義!! 不知道是否是因為WinCE上不提供這個API,只提供字串轉整數的atoi

由於我想把整數的值寫入文字檔,勢必要把值轉成字串才能寫入

google了一下,發現竟然有人自己寫itoa,不過我已經忘了我在哪看到的了

下面是itoa的function

void itoa(int num, char str[])
{
int sign = num,i = 0,j = 0;
char temp[11];

if(sign<0)//判斷是否是一個負數
{
num = -num;
}

do
{
temp[i] = num%10+'0';
num/=10;
i++;

}while(num>0);

if(sign<0)
{
temp[i++] = '-';
}

temp[i] = '';
i--;

while(i>=0)
{
str[j] = temp[i];
j++;
i--;
}

str[j] = '';
}

2009.4.7更新

其實WinCE上有替代的function,用法一樣

_itoa(tempint, tempchar, 16);


From C++ library:
Parameters:


value:
Number to be converted

string:
String result

radix:
Base of value; must be in the range 2 10 16

2009年3 月 posted by admin in 程式&軟體 and have No Comments

Place your comment

Please fill your data and comment below.
名稱:
信箱:
網站:
您的評論: