write variable to string

This is my first program in C++. I have written this same program in vb already. This seems like it should be very simple. I have this bit of code.

unsigned char hdr[2];
//the hdr array gets filled from another array

int i
for(i=0 ; i<2 ; i++)
{
fprintf(logfile, " %02", (unsigned int)hdr[i]);
}

This code will write something like "55BB" to the log file. Which is great, I am looking for "55BB". But what I really need is "55BB" in a string variable and not written to the log file. Funny thing is that I know 2 people that are C++ "experts" and this seems like a very difficult question. They always come back with more questions and no answers. Hopefully someone here will be able to help.
Perhaps a stringstream & some io manipulation will do the job:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

int main()
{
   unsigned char hdr[2];
   //the hdr array gets filled from another array
   hdr[0]='5';
   hdr[1]='B';

   int i;
   //for(i=0 ; i<2 ; i++)
   //{
   //   printf(" %02d", (unsigned int)hdr[i]);
   //}

   std::stringstream ss;
   for(i=0 ; i<2 ; i++)
   {
      ss << " " << std::setw(2) << (unsigned int)hdr[i];
   }
   std::string s;
   getline(ss, s);
   std::cout << s << std::endl ;
}
Thanks for the reply. I tried your code but i get the message that std::setw is not a member of 'std'.
Should work if you #include <iomanip>
that did work. Thanks. But now I realize that my result needs to be in hex. I got a result of 85 187, when what I really need is 55 BB. I guess that would be another topic. I will google that and see if I can come up with an answer.

Thanks Norm.
Do you want it in C++ string or in C-style character array?

In first case, use stringstreams, in second, snprintf.
Easy peasy:
1
2
3
4
   for(i=0 ; i<2 ; i++)
   {
      ss << std::hex << " " << std::setw(2) << (unsigned int)hdr[i];
   }
I guess I don't know the difference. I want 85 187 to be 55 BB. But I just changed some of my code to look for 85187 instead of 55BB.

Thanks for the fast responses. I am sure I will be back again soon. But I think I have enough to actually start writing this program.
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

int main()
{
   int i = 21947;

   std::stringstream ss;

   ss << std::hex << i;

   std::string s;
   getline(ss, s);
   std::cout << s << std::endl;
}


55bb
closed account (E0p9LyTq)
@flwhiterose,

85187 is hex 0x14cc3. 21947 is 0x55bb.
He has a two variables: unsigned char hdr[2];. One holds value of 85 (0x55) and second holds 187 (0xBB). He vants those to be printed in hex without separating space. That is all.
That is correct MiiNiPaa. But I just went ahead and compared to 85 and 187 instead of 55 and BB. I think it is really stupid and real C++ programers would probably laugh at my code, but it works.
Topic archived. No new replies allowed.