Wow
I can confirm michy's problem.
I just tried this on GCC on my Ubuntu machine and I don't get expected output at all.
What's more, I get different output when output to a file or to a stringstream:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int main(int argc,char* argv[])
{
char buf[10];
stringstream s;
s << hex << showbase << &buf[0];
string str = s.str();
for(unsigned i = 0; i < str.length(); ++i)
{
cout << hex << setfill('0') << setw(2) << int(str[i]) << " ";
}
return 0;
}
|
04 08 |
You might notice that 04 08 would not produce actual text.
When I output directly to cout, I get jibberish. I'd paste the jibberish in here but I can't for the life of me figure out how the hell you copy from this console. Ctrl+C just seems to close it and there's no right click menu. (grumble grumble Linux)
On a semi-unrelated note, I've noticed that GCC scrwes up other output (specifically from typeid). I wonder if this is just a bug in the compiler/lib?
EDIT:
when I print to an fstream, the file contains this:
14 B0 BC 09
Note this is when I view the file in a hex editor (ie: the file is 4 bytes in size). That 4 bytes is
not actual text, but is utter crap.
EDIT AGAIN:
Interestingly, blackcoder's suggestion of casting works fine:
1 2 3 4 5 6 7 8
|
int main(int argc,char* argv[])
{
char buf[10];
cout << hex << showbase << unsigned(&buf[0]); // expected output
return 0;
}
|
I get a warning about the cast, but it still compiles okay.