在C语言中将整数转换为字符串

43

我正在使用 itoa() 函数将一个 int 转换为 string,但是出现了错误:

undefined reference to `itoa'
collect2: ld returned 1 exit status

这是什么原因呢?是否有其他方法可以执行这种转换?


是的,我已经包含了 stdlib.h 头文件,但仍然无法正常工作。 - Shweta
可能是 如何在 C 中将 int 转换为字符串 的重复问题。 - Ciro Santilli OurBigBook.com
14个回答

0

像 Edwin 建议的那样,使用 snprintf

#include <stdio.h>
int main(int argc, const char *argv[])
{
    int n = 1234;
    char buf[10];
    snprintf(buf, 10, "%d", n);
    printf("%s\n", buf);
    return 0;
}

0

如果你真的想使用itoa函数,你需要包含标准库头文件。

#include <stdlib.h>

我也相信如果你正在使用 MSVC 的 Windows,则 itoa 实际上是 _itoa

请参见 http://msdn.microsoft.com/en-us/library/yakksftt(v=VS.100).aspx

再次提醒,由于您从 collect2 收到消息,因此您可能在 *nix 上运行 GCC。


0
if(InNumber == 0)
{
    return TEXT("0");
}

const int32 CharsBufferSize = 64; // enought for int128 type
TCHAR ResultChars[CharsBufferSize];
int32 Number = InNumber;

// Defines Decreasing/Ascending ten-Digits to determine each digit in negative and positive numbers.
const TCHAR* DigitalChars = TEXT("9876543210123456789");
constexpr int32 ZeroCharIndex = 9; // Position of the ZERO character from the DigitalChars.
constexpr int32 Base = 10; // base system of the number.

// Convert each digit of the number to a digital char from the top down.
int32 CharIndex = CharsBufferSize - 1;
for(; Number != 0 && CharIndex > INDEX_NONE; --CharIndex)
{
    const int32 CharToInsert = ZeroCharIndex + (Number % Base);
    ResultChars[CharIndex] = DigitalChars[CharToInsert];
    Number /= Base;
}

// Insert sign if is negative number to left of the digital chars.
if(InNumber < 0 && CharIndex > INDEX_NONE)
{
    ResultChars[CharIndex] = L'-';
}
else
{
    // return to the first digital char if is unsigned number.
    ++CharIndex;
}

// Get number of the converted chars and construct string to return.
const int32 ResultSize = CharsBufferSize - CharIndex;
return TString{&ResultChars[CharIndex], ResultSize};

2
如果您能解释答案而不仅仅是发布代码,那将是很好的。 - Deepak Kumar

0

itoa()函数在ANSI-C中未定义,因此在某些平台上默认未实现(参考链接)。

s(n)printf()函数是itoa()的最简替代品。然而,itoa(整数转ASCII)函数可用作更好的整数转ASCII转换问题的解决方案。

itoa()也比s(n)printf()更好,因为性能取决于实现。一个简化的itoa(仅支持10进制)实现示例:参考链接

另一个完整的itoa()实现如下(参考链接):

#include <stdbool.h> 
#include <string.h>

// A utility function to reverse a string 
char *reverse(char *str)
{
  char *p1, *p2;

  if (! str || ! *str)
        return str;
  for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
  }
  return str;
}
// Implementation of itoa() 
char* itoa(int num, char* str, int base) 
{ 
    int i = 0; 
    bool isNegative = false; 
  
    /* Handle 0 explicitely, otherwise empty string is printed for 0 */
    if (num == 0) 
    { 
        str[i++] = '0'; 
        str[i] = '\0'; 
        return str; 
    } 
  
    // In standard itoa(), negative numbers are handled only with  
    // base 10. Otherwise numbers are considered unsigned. 
    if (num < 0 && base == 10) 
    { 
        isNegative = true; 
        num = -num; 
    } 
  
    // Process individual digits 
    while (num != 0) 
    { 
        int rem = num % base; 
        str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; 
        num = num/base; 
    } 
  
    // If number is negative, append '-' 
    if (isNegative) 
        str[i++] = '-'; 
  
    str[i] = '\0'; // Append string terminator 
  
    // Reverse the string 
    reverse(str); 
  
    return str; 
} 

另一个完整的itoa()实现:参考链接 下面是itoa()的使用示例(参考链接):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    int a=54325;
    char buffer[20];
    itoa(a,buffer,2);   // here 2 means binary
    printf("Binary value = %s\n", buffer);
 
    itoa(a,buffer,10);   // here 10 means decimal
    printf("Decimal value = %s\n", buffer);
 
    itoa(a,buffer,16);   // here 16 means Hexadecimal
    printf("Hexadecimal value = %s\n", buffer);
    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接