我正在使用 itoa()
函数将一个 int
转换为 string
,但是出现了错误:
undefined reference to `itoa'
collect2: ld returned 1 exit status
这是什么原因呢?是否有其他方法可以执行这种转换?
我正在使用 itoa()
函数将一个 int
转换为 string
,但是出现了错误:
undefined reference to `itoa'
collect2: ld returned 1 exit status
这是什么原因呢?是否有其他方法可以执行这种转换?
snprintf
更加便携,比使用itoa
更可靠。itoa 不是 C 标准库或者 C++ 标准库的一部分,但是很多编译器和相关库都支持它。
sprintf
的例子。char* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);
snprintf
示例
char buffer[10];
int value = 234452;
snprintf(buffer, 10, "%d", value);
这两个函数类似于fprintf
,但输出写入数组而不是流中。 sprintf
和snprintf
的区别在于snprintf
通过写入缓冲区可以存储的最大字符数来保证不会发生缓冲区溢出。
sprintf
和snprintf
之间的另一个区别是它们所描述的标准:sprintf
适用于C89(及更高版本);snprintf
适用于C99。值得注意的是,Windows库缺少snprintf
函数或其行为与标准描述不同。 - pmgsnprintf
是否将'\0'
放置在我的数组最后一个元素中?这样我就可以知道它在哪里结束了? - Silidrone请使用snprintf
函数,该函数是标准的,并且在每个编译器中都可用。通过使用NULL, 0
参数调用该函数以查询所需的大小。为字符串末尾的空字符分配一个额外的字符。
int length = snprintf( NULL, 0, "%d", x );
char* str = malloc( length + 1 );
snprintf( str, length + 1, "%d", x );
...
free(str);
itoa
不是 ANSI 函数 - 它不是标准的 C 函数。你应该使用 sprintf
将一个 int
转换成一个字符串。
itoa
接受三个参数。char *
变量,程序可能会崩溃,所以你应该传递一个正常大小的 char 数组,它会很好地工作。itoa
是一个非常有用的函数,由一些编译器支持 - 很可惜不像 atoi
那样被所有编译器支持。itoa
,这里是如何使用它。否则,你还有另一个选择,使用 sprintf
(只要你想要输出 8、10 或 16 进制):char str[5];
printf("15 in binary is %s\n", itoa(15, str, 2));
char stringNum[20];
int num=100;
sprintf(stringNum,"%d",num);
char stringNum[21];
在 -power(2,63)
操作中需要。 - chux - Reinstate Monicasprintf
被认为是“不良实践”,也是内存损坏、安全问题/漏洞和其他奇怪错误的最常见根本原因之一。如果可能的话,每当您将字符串格式化到缓冲区时,请始终使用snprintf
,并确保您跟踪所使用的缓冲区的大小(还要考虑尾随\0!) - origoitoa
函数:void my_utoa(int dataIn, char* bffr, int radix){
int temp_dataIn;
temp_dataIn = dataIn;
int stringLen=1;
while ((int)temp_dataIn/radix != 0){
temp_dataIn = (int)temp_dataIn/radix;
stringLen++;
}
//printf("stringLen = %d\n", stringLen);
temp_dataIn = dataIn;
do{
*(bffr+stringLen-1) = (temp_dataIn%radix)+'0';
temp_dataIn = (int) temp_dataIn / radix;
}while(stringLen--);}
这是一个例子:
char buffer[33];
int main(){
my_utoa(54321, buffer, 10);
printf(buffer);
printf("\n");
my_utoa(13579, buffer, 10);
printf(buffer);
printf("\n");
}
snprintf()
是最好的选择:char str[16]; // could be less but i'm too lazy to look for the actual the max length of an integer
snprintf(str, sizeof(str), "%d", your_integer);
char string[something];
sprintf(string, "%d", 42);
void itos(int value, char* str, size_t size) {
snprintf(str, size, "%d", value);
}
..与 按引用传递 一起工作。例如这样使用:
int someIntToParse;
char resultingString[length(someIntToParse)];
itos(someIntToParse, resultingString, length(someIntToParse));
resultingString
将保存你的C-“字符串”。此外,以下实现避免了两次执行模块/除操作。
char *u32todec( uint32_t value,
char *buf,
int size)
{
if(size > 1){
int i=size-1, offset, bytes;
buf[i--]='\0';
do{
buf[i--]=(value % 10)+'0';
value = value/10;
}while((value > 0) && (i>=0));
offset=i+1;
if(offset > 0){
bytes=size-i-1;
for(i=0;i<bytes;i++)
buf[i]=buf[i+offset];
}
return buf;
}else
return NULL;
}
int main(void)
{
uint64_t acc;
uint32_t inc;
char buf[16];
size_t bufsize;
for(acc=0, inc=7; acc<0x100000000; acc+=inc){
printf("%u: ", (uint32_t)acc);
for(bufsize=17; bufsize>0; bufsize/=2){
if(NULL != u32todec((uint32_t)acc, buf, bufsize))
printf("%s ", buf);
}
printf("\n");
if(acc/inc > 9)
inc*=7;
}
return 0;
}
看这个例子
#include <stdlib.h> // for itoa() call
#include <stdio.h>
int main() {
int num = 145;
char buf[5];
// convert 123 to string [buf]
itoa(num, buf, 10);
// print our string
printf("%s\n", buf);
return 0;
}
看看这个链接,其中有其他的例子。