Sign in to follow this  

how to convert a short array to a string??

This topic is 3588 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 decided to try a general method of converting a short array to a string. here is my code.

short myArray = new short[10];

for(int i = 0; i < 10; i++)
  myArray[i] = i *10;

ostringstream myString;

myString << myArray;

cout << "myArray: ";
for(int i = 0; i < 10; i++)
  cout << myArray[i] << " ";

cout << endl;

cout << "myString: " << myString.str() << endl;


output:
Quote:
myArray: 0 10 20 30 40 50 60 70 80 90 myString: 0x8050000
obviously i used the ostringstream stuff wrong. what did i do wrong? thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Tell me: Why did you output myArray into myStream one way, and output it into cout another way?



i dont understand what you are saying.

i was using the << operator on the stream to convert it to a string like you can do with other types.

what did you mean?

Share this post


Link to post
Share on other sites
Sneftel asks because ostringstream behaves just like cout (only to a string, not standard output) and you managed to get it right with cout, so why did you get it confused with ostringstream?

I think you misunderstand C/C++ arrays:
C++ arrays are very minimal - they are just a pointer to the first element. When you send an array to a function you are actually passing a pointer to the first element. The function has no way to know how large the array is without extra info.

So when you passed the array to ostringstream it does what you would expect it to do with a pointer - it translates the address to a printable string.

If you want to print the array you need to feed ostringstream one array element at a time, like you did with cout.

You can wrap the array with a class ("MyArray") and then make a function operator<<(stream&, const MyArray&) which would have the loop, and then use that function to print your arrays.

Share this post


Link to post
Share on other sites
Quote:
Original post by OpenGL_Guru
Quote:
Original post by Sneftel
Tell me: Why did you output myArray into myStream one way, and output it into cout another way?



i dont understand what you are saying.

i was using the << operator on the stream to convert it to a string like you can do with other types.

what did you mean?


You have two streams: myString and cout. You tried to output myArray to both of these streams. You did it two different ways. Which way worked? Which way didn't?

Share this post


Link to post
Share on other sites
A stringstream behaves just like a console stream, which is what std::cout is. Let's obscure the variable names, and see if you can now recognize the problem:


foo << myArray;

for(int i = 0; i < 10; i++)
bar << myArray[i] << " ";

// Why do we see different things in the two streams?

Share this post


Link to post
Share on other sites
well i think i got what i wanted now. not the prettiest thing in the world.


string str;
stringstream out;

short myArray = new short[10];

for(int i = 0; i < 10; i++)
{
myArray[i] = i * 10;
out << myArray[i];
str = out.str();
out.clear();
}

cout << "myArray: ";

for(int i = 0; i < 10; i++)
cout << myArray[i] << " ";

cout << endl << "string array: " << str << endl;







output:

Quote:


myArray: 0 10 20 30 40 50 60 70 80 90
string array: 0102030405060708090



looks like that worked fine. thanks all for the help. i guess i really didnt need the ostringstream stuff. i just needed to get the shorts into a string format, i guess in essence i was making it too hard.

Share this post


Link to post
Share on other sites
Quote:
Original post by OpenGL_Guru
well i think i got what i wanted now. not the prettiest thing in the world. ... i guess i really didnt need the ostringstream stuff.

OMG... here's a less subtle clue. Replace "cout" with "myString" in your first example.

EDIT: At this point you have something that works for you, so I'll just show you what people were trying to explain this whole time. You had it working with cout... to get it to work with the stringstream you just had to do EXACTLY what you did with cout.

ostringstream myString;
for(int i = 0; i < 10; i++)
mystring << myArray[i] << " ";

Share this post


Link to post
Share on other sites

This topic is 3588 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this