2008-12-07 115 views

回答

22

首先转换为int使用sprintf()一个char*

char integer_string[32]; 
int integer = 1234; 

sprintf(integer_string, "%d", integer); 

然后将其附加到其他的char *,使用strcat()

char other_string[64] = "Integer: "; // make sure you allocate enough space to append the other string 

strcat(other_string, integer_string); // other_string now contains "Integer: 1234" 
+3

这也适用于C. – Sydius 2008-12-07 02:46:24

9

你也可以使用stringstreams。

width = floor(log10(num))+1; 
result = malloc(strlen(str)+len)); 
sprintf(result, "%s%*d", str, width, num); 

你可以使用的最大长度为您的系统上一个整数简化长度:

char *theString = "Some string"; 
int theInt = 5; 
stringstream ss; 
ss << theString << theInt; 

的字符串,然后可以使用ss.str();

4

喜欢的东西进行访问。

编辑 oops - 没有看到“++”。不过,这是另一种选择。