Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put integer values into a char array

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
US
I have a string that I would like to update with some values from some calculations. These calculations result in the ASCII values of the numbers 0 thru 9 and letters A through Z. They are stored in an integer variable and I would like to place these values into the string (char array) as characters. Is there a simple way to do this conversion (I guess that is what I'd call it)? I've attempted to use a sprintf, but I was unable to get it to compile...other methods have been unsuccessful, as well.

Thanks for your time.
 
If you have integer values and want to store them in a char array ... just do it! ;)

Code:
int result = '9'; // ascii code of character '9'
char array[5];
array[0] = result; // implicit int to char conversion

Was that it?
Greetings,
Tobi
 
How did you try to call sprintf? If I understand your question correctly, that should have worked.

The call should look something like:

sprintf( dest_str, "An integer value: %d", some_int );


A C++ier way to do the same thing involves using stringstream (or the deprecated strstream; usage is similar, but uses char* instead of std::string).

stream << &quot;An integer value: &quot; << some_int;
stream.str().c_str(); // this call gets you the char*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top