Question:
c++ question, int to char?
a.h
2009-06-12 09:52:49 UTC
how do you convert an int to a char in c++?
let's say that there is an int x and a char y
now, let's say that i want x=25, how do i make it so that that will define y as "25"?
Six answers:
2009-06-12 10:00:50 UTC
First, a char wouldn't be big enough to handle the conversion as the converted data is 2 chars in width. You are best to make y a string or a char[3] and use itoa



http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
obey
2016-12-14 08:58:25 UTC
in the adventure that your compiler helps it itoa is the least complicated: char text textile[20]; itoa(n,text textile,10); yet it relatively is particularly gadget based. char text textile[20]; sprintf(text textile,"%d",n); Is extra wide-unfold and versatile (you may define formatting and so on...) the two have ability overflow matters, you may desire to make optimistic text textile[] is a minimum of one byte longer than the form of digits in the form you're changing. considering the fact that an int won't be able to be 20 characters long the above could be secure. in case you opt for to be paranoid or haven't any theory how long the ensuing string could desire to be because of the fact it is composed of distinctive numbers then use snprintf considering the fact that that enables you to specify the dimensions of the char array that is outputting to. If the text textile is in simple terms too long it will in simple terms decrease the tip off extremely than crash or open a ability secure practices hollow. #define TEXT_SIZE 10 char text textile[TEXT_SIZE]; snprintf(text textile, TEXT_SIZE, "%d", n);
2009-06-12 10:17:11 UTC
hi a.h, i can explain it with an example...i hope it will clarify whether u r on mac or wateva.

Lets take

Code:int dig = 123;

to convert it you should pic 3, 2 and 1 seperately. Using this

Code:int countVar=dig;



for(int numOfDigits=0; countVar !=0;numOfDigits++)

countVar=countVar/10;



char char_array[100]; //Or you can use malloc(): char* char_array=(char*)malloc(numOfDigits+1);



for(int i=numOfDigits-1;i>=0;i--)

{

char_array[numOfDigits-i-1]=char(dig%int(pow(10,(i+1)))/pow(10,i))+'0'; //Plus '0' to convert int to char

}

char_array[numOfDigits]=NULL;





Tell me if it didn't work. another situation can be:

#include



int main(void)

{

char text[11];

int value = 123;

sprintf(text, "%d", value);

printf("text = \"%s\"\n", text);

return 0;

}



/* my output

text = "123"

*/
Rakesh M
2009-06-12 10:05:12 UTC
the function is: itoa();



here's an example:



#include

#include



void main()

{

int num = 1423;

char string[25];



itoa(number, string, 10); //10 is the base of the number

printf("String: %s", string);

}
2009-06-12 11:52:05 UTC
try



int x = 20;

char y = (char)x;



I haven't programmed for a while...
2009-06-12 11:20:23 UTC
Use type conversion operators.



http://xoax.net/comp/cpp/console/Lesson25.php


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...