質問
C ++のchar*
に整数を追加するにはどうしますか?
解決
最初にchar*
を使用してintをsprintf()
に変換します:
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"
他のヒント
文字列ストリームも使用できます。
char *theString = "Some string";
int theInt = 5;
stringstream ss;
ss << theString << theInt;
その後、ss.str();
次のようなもの:
width = floor(log10(num))+1;
result = malloc(strlen(str)+len));
sprintf(result, "%s%*d", str, width, num);
システムの整数の最大長を使用すると、lenを単純化できます。
編集おっと-<!> quot; ++ <!> quot;が表示されませんでした。それでも、それは代替案です。
所属していません StackOverflow