Integers to strings.

Started by
11 comments, last by GekkoCube 22 years, 3 months ago
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 ~
Advertisement
C: See itoa().
C++: See stringstream.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
C: Not sure is itoa() is a standard function, try sprintf().
StringStream whats that? I''ve always used itoa... is there a better way in c++?
#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
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
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
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 ~
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

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
||--------------------------||
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||

This topic is closed to new replies.

Advertisement