如何将int转换为C中的string

如何将int (整数)转换为string? 我试图做一个函数,将一个struct的数据转换成一个string保存在一个文件中。

编辑:正如在评论中指出, itoa()不是一个标准,所以更好的使用sprintf()方法build议在对手的答案!


您可以使用itoa()函数您的整数值转换为string。

这里是一个例子:

 int num = 321; char snum[5]; // convert 123 to string [buf] itoa(num, snum, 10); // print our string printf("%s\n", snum); 

如果你想输出你的结构到一个文件中,没有必要事先转换任何值。 您可以使用printf格式规范来指示如何输出值,并使用printf系列中的任何运算符输出数据。

你可以使用sprintf来完成它,也可以使用snprintf如果有的话):

 char str[ENOUGH]; sprintf(str, "%d", 42); 

str的字符数(加上终止字符)可以使用下面的公式计算:

 (int)((ceil(log10(num))+1)*sizeof(char)) 

简短的回答是:

 snprintf( str, size, "%d", x ); 

时间越长,首先你需要find足够的大小。 如果你用NULL, 0作为第一个参数调用snprintf告诉你长度:

 snprintf( NULL, 0, "%d", x ); 

为null结束符分配一个字符。

 int x = -42; int length = snprintf( NULL, 0, "%d", x ); char* str = malloc( length + 1 ); snprintf( str, length + 1, "%d", x ); ... free(str); 

如果适用于每个格式string,那么可以使用"%g"将float或double转换为string,可以使用"%x"将int转换为hex等等。

查看了各种版本的itoa for gcc之后,我发现最灵活的版本能够将转换处理为二进制,十进制和hex,正反两方面都是http://www.strudel.org上的第四个版本。; .uk / itoa / 。 虽然sprintf / snprintf有优势,但除了十进制转换之外,它们不会处理负数。 由于上面的链接是离线或不再活跃,我已经包括他们的第四个版本如下:

 char * itoa (int value, char *result, int base) { // check that the base if valid if (base < 2 || base > 36) { *result = '\0'; return result; } char* ptr = result, *ptr1 = result, tmp_char; int tmp_value; do { tmp_value = value; value /= base; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; } while ( value ); // Apply negative sign if (tmp_value < 0) *ptr++ = '-'; *ptr-- = '\0'; while (ptr1 < ptr) { tmp_char = *ptr; *ptr--= *ptr1; *ptr1++ = tmp_char; } return result; } 

这是旧的,但这是另一种方式。

 #include <stdio.h> #define atoa(x) #x int main(int argc, char *argv[]) { char *string = atoa(1234567890); printf("%s\n", string); return 0; } 

将任何东西转换为string应该1)分配结果string或2)通过char *目的地和大小。 下面的示例代码:

两者都适用于包括INT_MIN在内的所有int 。 它们提供了一个一致的输出,而snprintf()取决于当前的语言环境。

方法1:在内存不足时返回NULL

 #define INT_DECIMAL_STRING_SIZE(int_type) ((CHAR_BIT*sizeof(int_type)-1)*10/33+3) char *int_to_string_alloc(int x) { int i = x; char buf[INT_DECIMAL_STRING_SIZE(int)]; char *p = &buf[sizeof buf - 1]; *p = '\0'; if (i >= 0) { i = -i; } do { p--; *p = (char) ('0' - i % 10); i /= 10; } while (i); if (x < 0) { p--; *p = '-'; } size_t len = (size_t) (&buf[sizeof buf] - p); char *s = malloc(len); if (s) { memcpy(s, p, len); } return s; } 

方法2:如果缓冲区太小,则返回NULL

 static char *int_to_string_helper(char *dest, size_t n, int x) { if (n == 0) { return NULL; } if (x <= -10) { dest = int_to_string_helper(dest, n - 1, x / 10); if (dest == NULL) return NULL; } *dest = (char) ('0' - x % 10); return dest + 1; } char *int_to_string(char *dest, size_t n, int x) { char *p = dest; if (n == 0) { return NULL; } n--; if (x < 0) { if (n == 0) return NULL; n--; *p++ = '-'; } else { x = -x; } p = int_to_string_helper(p, n, x); if (p == NULL) return NULL; *p = 0; return dest; } 

[编辑]由@Alter Mann请求

(CHAR_BIT*sizeof(int_type)-1)*10/33+3至less是将某些有符号整数types编码为string所需的最大char数,该string由可选的负号,数字和空字符组成。

有符号整数中的非符号位数不超过CHAR_BIT*sizeof(int_type)-1n位二进制数的10进制表示占用n*log10(2) + 1数字。 10/33略高于log10(2) 。 符号char为+1,空字符为+1。 其他部分可以像28/93一样使用。


方法3:如果一个人想要活在边缘,缓冲区溢出不是一个问题,那么一个简单的C99或更高版本的解决scheme将处理所有的 int

 #include <limits.h> #include <stdio.h> static char *itoa_simple_helper(char *dest, int i) { if (i <= -10) { dest = itoa_simple_helper(dest, i/10); } *dest++ = '0' - i%10; return dest; } char *itoa_simple(char *dest, int i) { char *s = dest; if (i < 0) { *s++ = '-'; } else { i = -i; } *itoa_simple_helper(s, i) = '\0'; return dest; } int main() { char s[100]; puts(itoa_simple(s, 0)); puts(itoa_simple(s, 1)); puts(itoa_simple(s, -1)); puts(itoa_simple(s, 12345)); puts(itoa_simple(s, INT_MAX-1)); puts(itoa_simple(s, INT_MAX)); puts(itoa_simple(s, INT_MIN+1)); puts(itoa_simple(s, INT_MIN)); } 

示例输出

 0 1 -1 12345 2147483646 2147483647 -2147483647 -2147483648 

如果您使用GCC,则可以使用GNU扩展asprintf函数。

 char* str; asprintf (&str, "%i", 12313); free(str); 

如果你想输出你的结构到一个文件中,没有必要事先转换任何值。 您可以使用printf格式规范来指示如何输出值,并使用printf系列中的任何运算符输出数据。

 /*Function return size of string and convert signed * *integer to ascii value and store them in array of * *character with NULL at the end of the array */ int itoa(int value,char *ptr) { int count=0,temp; if(ptr==NULL) return 0; if(value==0) { *ptr='0'; return 1; } if(value<0) { value*=(-1); *ptr++='-'; count++; } for(temp=value;temp>0;temp/=10,ptr++); *ptr='\0'; for(temp=value;temp>0;temp/=10) { *--ptr=temp%10+'0'; count++; } return count; }