Archived

This topic is now archived and is closed to further replies.

Integers to strings.

This topic is 5822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought I''d post this rather than expend my energy figuring this one out. I''m trying to convert an integer to a "string" (character array). I can convert characters to "strings" easily, simply because of the nature of the character array. But when it comes to intergers, the integer gets converted to ascii or something. I tried typecasting it, but that doesnt seem to solve my problem. Heres a brief snippet of what im doing: #include char array[100]; int age = 24; char sex = ''M''; array[0] = sex; array[1] = age; array[2] = ''\0''; string str(array); // This will print out as "M" followed by a junk-character because of the mis-converted variable. How can this be done??? Thanks. ~ I''''m a wannabe programmer ~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
C: Not sure is itoa() is a standard function, try sprintf().

Share this post


Link to post
Share on other sites

#include
#include
#include
.
int main(void)
{
std::stringstream sstrm;
int i = 42;
std::string str = "The meaning of life is ";
sstrm << str << i << '\n';
std::cout << sstrm.str;
std::cout.flush();
return 0;
}


[Edit: References]
I thought someone might also find these useful:
[DevX] Automating TypeConversions with stringstream Objects
[cplusplus.com] C++ reference: stringstream

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!


Edited by - Oluseyi on December 31, 2001 10:11:32 PM

Share this post


Link to post
Share on other sites
if your not supposed to use a library function, you can write your own algorithm. im not goinu post it for you, but ill give you a hint:

if you want to get the rightmost digit,

int i = 1245;
int RightMost = i % 10;

now, you could put that in a char like this

char C = RightMost + '0'; //= '5'

!!
///sorry, its %, not mod!

Edited by - evilcrap on December 31, 2001 11:16:19 PM

Share this post


Link to post
Share on other sites
I haven't checked the ASCII chart lately, but all digits in ASCII are just that digit plus 0x30. So in ASCII, 1 = 0x31, 2 = 0x32, etc. More accurately, '1' = 0x31, '2' = 0x32, etc. The 0x implies that the number is in hexadecimal form.

Edited by - masonium on December 31, 2001 11:13:05 PM

Share this post


Link to post
Share on other sites
Holy moly.

All this sounds like overkill to me.
There''s gotta be an easier way.

The strings I am using are the STL string.
So I guess I can create a regular array of char''s, then use sprintf to convert anything I need to an array, and then print them out.

- This method seems easier than all the previously posted stuff.
Or am i missing something?


~ I''''m a wannabe programmer ~

Share this post


Link to post
Share on other sites
Hey.

I assume what you are trying to do is turn the int that has the value of 24 to a string reading "24"? If I am wrong just ignore this, but if I am correct, this should do the trick.


char *temp[3];
sprintf(temp, "%d", age);


Hopefully that will work.

Minion

Share this post


Link to post
Share on other sites
Not sure if this is right but try this
char Array[100];
sprintf(Array, "%c %d", sex, age);
%c would be character and %d should be the integer age
I don''t remember my C thingys for printf so you''ll have to find the right one for a character and a integer but thats how i would do it. Hope that helps somewhat

||--------------------------||
Black Hole Productions
President/Programmer/Expert on stuff
||--------------------------||

Share this post


Link to post
Share on other sites
quote:
Original post by GekkoCube
this method seems easier than all the previously posted stuff.

Doesn''t beat stringstream.
quote:
Or am i missing something?

stringstream (I''m starting to feel like that AFLAK duck...). Follow the links I posted, especially the DevX one; it gives a number of really good examples.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by max621
sprintf(Array, "%c %d", sex, age);

An excellent example why stringstream is superior: you don''t have to remember/lookup no fecking format specifiers!
int age = 120;
char sex = ''M'';
string str;
stringstream sstrm;
sstrm << "Age: " << age << " Sex: " << sex;
sstrm >> str;
cout << str << endl;

You could also have done ''cout << sstr.str() << endl;'' or so.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites

Hey thanks everyone!

I remember using sprintf...I just havent used it in such a long time. Thanks Minion, thats exactly what I''ve been looking for, sprintf()!

Also thanks for the stringstream tip. That does seem to beat the sprintf(), although im more comfortable with it.

so special thanks to Minion and the AFLAK guy.


~ I''''m a wannabe programmer ~

Share this post


Link to post
Share on other sites