Solved

convert int to char[] and store in stringstream

Posted on 2009-07-13
30
1,152 Views
Last Modified: 2013-11-17
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

0
Comment
Question by:rajaki
  • 11
  • 9
  • 5
  • +4
30 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24846555
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
 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24846582
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24846583
Do you want to display the int value in hexadecimal ? Or otherwise ?

Can you show us which output you expect ?
0
 

Author Comment

by:rajaki
ID: 24846586
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
 

Author Comment

by:rajaki
ID: 24846623
i dont have any specific preference for using stringstream.
what else can i use so that i can print all these characters?
0
 

Author Comment

by:rajaki
ID: 24846633
Hi infinity08,

i just want to convert the int into bytes and display each byte
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24846638
>> i just want to convert the int into bytes and display each byte

As a byte ? ie. not in a human-readable form ?
0
 

Author Comment

by:rajaki
ID: 24846646
thats right. i need to handle it on a byte level.
0
 
LVL 9

Expert Comment

by:masheik
ID: 24846648
0
 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24846663
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24846674
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
 
LVL 12

Expert Comment

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

Open in new window

0
 

Author Comment

by:rajaki
ID: 24846721
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
 

Author Comment

by:rajaki
ID: 24846723
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24846742
> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24846744
>> 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
 

Author Comment

by:rajaki
ID: 24846788
This is the output :  (which i got in the file).
^@^@^@^E

0
 

Author Comment

by:rajaki
ID: 24846796
Hi Let me be,

I got blanks as output.
0
 
LVL 14

Expert Comment

by:flob9
ID: 24846894
^@^@^@^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
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 24846918
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24847013
>> 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
 

Author Comment

by:rajaki
ID: 24850175
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24850334
>> 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
 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 24850379
Assuming of course the platform you are using does have sizeof(int) <= 4.
0
 

Author Comment

by:rajaki
ID: 24850538
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24850657
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24906753
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
 

Author Comment

by:rajaki
ID: 24906849
sorry about that. the solution is clear. how can i change the grade?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24906959
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now