convert int to char[] and store in stringstream

i am trying to convert an int into a char[] and store the char array in a stringstream.
but when i try to print out the characters, i dont see any and even the stringstream is empty. where am i going wrong?




stringstream ss;
int d = 5;
char* c;
c = new char[4];
c[0] = (char) (((d) >> 24) & 0xf);
c[1] = (char) (((d) >> 16) & 0xf);
c[2] = (char) (((d) >> 8) & 0xf);
c[3] = (char) (((d) >> 0) & 0xf);
 
ss.write(c[0]); 
ss.write(c[1]);
ss.write(c[2]);
ss.write(c[3]);
 
cout << c[0] << ", " << c[1] << ", " << c[2] << ", " << c[3] << endl;
cout << "stringstream: " << ss.str();
 
none of the cout statements print anything. any help would be useful.

Open in new window

rajakiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Infinity08Connect With a Mentor Commented:
Then why not just :
unsigned int d = 5;
unsigned char buf[4];
buf[0] = (unsigned char) (((d) >> 24) & 0xFF);          /* <-- note the use of 0xFF instead of just 0xF */
buf[1] = (unsigned char) (((d) >> 16) & 0xFF);
buf[2] = (unsigned char) (((d) >> 8) & 0xFF);
buf[3] = (unsigned char) (((d) >> 0) & 0xFF);
 
std::cout.write(buf, sizeof(buf));
 
/* note that the output will not be human readable in general */
 
/* or even just the following if you don't care about endianness : */
 
unsigned int d = 5;
std::cout.write((char*) &d, sizeof(d));

Open in new window

0
 
ozoCommented:
Most of the characters will be the null character, except for  c[3]  which will be a control-E
Are you displaying the output in a way that makes null characters and control characters visible?
0
 
Let_Me_BeCommented:
Once you have a null character in the string, stringstream won't work. Definitely not using ss.str();

Why do you want to use stringstream?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Infinity08Commented:
Do you want to display the int value in hexadecimal ? Or otherwise ?

Can you show us which output you expect ?
0
 
rajakiAuthor Commented:
Hi ozo,

thanks for the reply.
yes, i expect c[3] to be control-E. but it doesn't get printed on the screen.
however, if i use
ofstream o("a.txt");
o << ss.str();

i can see control-E in the a.txt file. Why doesn't cout << ss.str(); display anything?

I want to be able to print these special characters (control-E in this case).
0
 
rajakiAuthor Commented:
i dont have any specific preference for using stringstream.
what else can i use so that i can print all these characters?
0
 
rajakiAuthor Commented:
Hi infinity08,

i just want to convert the int into bytes and display each byte
0
 
Infinity08Commented:
>> i just want to convert the int into bytes and display each byte

As a byte ? ie. not in a human-readable form ?
0
 
rajakiAuthor Commented:
thats right. i need to handle it on a byte level.
0
 
Let_Me_BeCommented:
Isn't something like this enough?
#include <iomanip>
#include <iostream>
using namespace std;
 
template < typename T >
void printhex(const T& v)
{
        const char * x = reinterpret_cast<const char*>(&v);
        for (int i = 0; i < sizeof(T); i++)
                cout << hex << static_cast<int>(x[i]);
}
 
 
int main()
{
        int a = 10;
        printhex(a);
}

Open in new window

0
 
Let_Me_BeCommented:
Sorry, one glitch, the output line should be:
cout << hex << setfill('0') << setw(2) << static_cast<int>(x[i]);

Open in new window

0
 
rajakiAuthor Commented:
Hi infinity08,

Thanks for the solution. is there a way to print out the special characters which i expect (like control-E in this case)? I dont see anything on the screen.
0
 
rajakiAuthor Commented:
Hi Let me be,

Thanks for the solution. I dont want to print it in hex format. i just want  to extract each byte and print it.
0
 
Let_Me_BeCommented:
> Thanks for the solution. I dont want to print it in hex format. i just want  to extract each byte and print it.

You just said, that you don't want to do that :-D

If you want to do it so, then change the line to:
if (isprint(x[i]) && (!isspace(x[i]))) cout << x[i]; else cout << ' ';

Open in new window

0
 
Infinity08Commented:
>> I dont see anything on the screen.

That's normal, because none of the characters can be displayed on the screen. That's why I said it wouldn't be human readable.

If you redirect standard output to a file however, you should get the same file as you got before.

Which output did you expect ? Can you paste it here ?
0
 
rajakiAuthor Commented:
This is the output :  (which i got in the file).
^@^@^@^E

0
 
rajakiAuthor Commented:
Hi Let me be,

I got blanks as output.
0
 
flob9Commented:
^@^@^@^E is the interpretation of the four bytes 0,0,0,5 that gives your editor.

Open your file with an hexadecimal editor and you will see : 00 00 00 05

(wich is, in my opinion, the best way to display binary data)
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
Here all the 3 bytes are becoming 0 which equevallent char is space.And in ur code c[3] is 5 which equevallent char is a special char. So need to change ur code as  follows...

c[0] = (char) (((d << 24)>>24) & 0xff);
c[1] = (char) (((d << 16)>>24) & 0xff);
c[2] = (char) (((d << 8)>>24) & 0xff);
c[3] = (char) ((d >> 24) & 0xff);
0
 
Infinity08Commented:
>> This is the output :  (which i got in the file).
>> ^@^@^@^E

Try using the code I posted, then redirect standard output to a file, and display that file in the same way you did to get that output, and you should get the same output. The thing is that that output is only there because of the text editor you used to open the file ... they are not the actual contents of the file - it's simply an interpretation, and you won't get the same interpretation when simply executing the code like this.

To redirect standard output to a file, call your executable like this :

        executable > out.txt


Maybe if you could explain why you want this specific output, we'd be able to assist you better ...
0
 
rajakiAuthor Commented:
ok, here is the thing. i need to store 2 integer numbers together as exactly 8 bytes. hence i was breaking the numbers into bytes and trying to store them so that i can manipulate them byte-by-byte later.
0
 
Infinity08Commented:
>> ok, here is the thing. i need to store 2 integer numbers together as exactly 8 bytes.

Then what was suggested in http:#24846674 should be sufficient.
Except that you might want to write directly to a file instead of to standard output.
0
 
Let_Me_BeCommented:
Assuming of course the platform you are using does have sizeof(int) <= 4.
0
 
rajakiAuthor Commented:
yes, the sizeof(int) is 4 in the platform i am using.
any suggestions how can  i read the value from the file and maybe do a sprintf/stringstream kind of thing?
thanks for the help.
0
 
Infinity08Commented:
>> any suggestions how can  i read the value from the file

Use read instead of write :

        http://www.cplusplus.com/reference/iostream/istream/read/


>> and maybe do a sprintf/stringstream kind of thing?

For what purpose ?
0
 
Infinity08Commented:
May I ask why you gave a B grade ? That usually means that something is still unclear and/or that something was missing in the answer. If so, then please feel free to ask for clarification where needed.
0
 
rajakiAuthor Commented:
sorry about that. the solution is clear. how can i change the grade?
0
 
Infinity08Commented:
No worries. I just wanted to make sure that your question was fully answered. If you want to change the grade, this help topic explains how you can do that :

        http://www.experts-exchange.com/help.jsp?hi=404
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.