Integers and Char Arrays!
-
Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael
-
Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael
check the sprintf() function. eg:
int n = 123456; char c[33] = {0}; sprintf( c, "%d", n );
nave [OpenedFileFinder]
-
check the sprintf() function. eg:
int n = 123456; char c[33] = {0}; sprintf( c, "%d", n );
nave [OpenedFileFinder]
Legend :-) Worked like a charm! Thanks
-
Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael
you can also use itoa, _itot function!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael
You can use
#include const int number = 47;
std::stringstream stream;
stream << number;std::string numberAsString = stream.str();
This way, you are using the power of the STL for you, and are avoiding literally thousands of error possibilities when fiddeling around with char-pointers and string lenghts. The std::string would fit in nicely with the std::vector (or any oher standard container), and as a plus you can get the content as a plain old C-String by calling its c_str()-member. Just to bring regular C++ to the mind, instead of this ugly 30+years old C code floating around here.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words