문제

당신은 어떻게 정수를 a char* C ++에서?

도움이 되었습니까?

해결책

먼저 int를 a로 변환합니다 char* 사용 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"

다른 팁

StringStreams를 사용할 수도 있습니다.

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을 단순화 할 수 있습니다.

편집하다 죄송합니다 - "++"을 보지 못했습니다. 그래도 대안입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top