Cannot display in hexadecimal - c++

unsigned int i = 0x02081;
cout << std::hex << i;
This displays 2081 when compiled with VS2010 but I think it should display 0x02081. Am I right, and if so, how can this be fixed?

By default the base is not printed:
cout << std::hex << std::showbase << i;

The easiest solution, of course, is:
cout << "0x" << std::hex << i;
Leading zeroes can vary in amount because they don't matter. You can choose any amount you like.

I think it should display 0x02081. Am
I right
No, you're not. It will display the value in hex, which is 2081. The 0x isn't part of the number, per se, it's just a notational convenience. The leading zero is also not a part of the number.
If you want the exact output you said you expected, you can do this:
cout << std::hex << std::showbase << std::setw(5) << std::setfill('0') << i;

It should display the value of i as a hexadecimal number - and does so. The prefix 0x is just something some programming languages use to indicate that a literal should be considered hexadecimal.
cout << "0x" << std::hex << i;
As Let_Me_Be points out: "0x" << std::hex could be replaced with std::hex << std::showbase if you want automatic printing of 0x/0/nothing for hex/octal/decimal.

Related

std::hex keeps the values not to change

I have some confusions in my codes regarding std:: functions.
cout<<tmp;
cout<<std::hex<<setw(4)<<tmp;
tmp is an int variable. After setting tmp to hex type, when I print tmp again, it is not converted back to decimal type (In face I have to include std::dec). Can anyone explain for me what happened behind-the-scene?
Thanks in advance
std::hex is sticky. You'll need to use std::dec to change the settings so that subsequent integral numbers are displayed in decimal.
cout << std::hex << setw(4) << tmp;
cout << std::dec << setw(4) << tmp;

inconsistent behavior of <iomanip>

I have the following code
cout << setfill('0') << setw(4) << hex << 100 << 100 << std::endl;
The output is:
006464
If I want to let every number with width 4, I have to use
out << setfill('0') << setw(4) << hex << 100 << sew(4) << 100 << std::endl;
But if I want to print every number with hex and setfill('0'), I only need to set setfill('0') and std::hex once.
Does c++ design this on purpose? what is its intention?
Yes it is on purpose. The stream operations are internally peppered with resets of the field width, specified by the standard. I think there's no good answer as to why.

Integer output formatting. what is the analogue of printf(%.3x)?

I did my googling for this thing, but haven't found the answer.
I want to find analogue for output formatting in plain C. To be more specific, something which works similar to printf(%.3x)
Probably, it could be done using manipulators. However, the code
cout << showbase << setfill('0') << setw(5) << hex << 19 << endl;
gives me 00x13 instead of desired 0x013.
P.S. Sorry, I don't have the Boost library, so this is not a solution..
Utilizing internal:
cout << showbase << setw(5) << setfill('0') << internal << hex << 19 << endl;
cout << "0x" << setfill('0') << setw(3) << hex << 19 << endl;
Note that setfill and hex alter the state of the stream for subsequent output as well, unlke setw which just affects the next output.
char buffer[40];
snprintf(buffer, 40, "%.3x", 19);
std::cout << buffer;

Why does this `int` have the wrong value?

Not sure how else to put that, but I'll start off with a code snippet and output:
uint32_t expires;
cout << "Expiration bytes: " << setfill('0') << hex
<< setw(2) << (unsigned short)rec[keyLen+4]
<< setw(2) << (unsigned short)rec[keyLen+5]
<< setw(2) << (unsigned short)rec[keyLen+6]
<< setw(2) << (unsigned short)rec[keyLen+7] << endl;
expires = ntohl(*(uint32_t*)&rec[keyLen+4]);
cout << "Expiration: " << (long)expires << endl;
cout << "Hex: " << hex << expires << endl;
Outputs:
Expiration bytes: 00000258
Expiration: 258
Hex: 258
I can confirm from other parts of the program that examining and outputting the hex representation of bytes works as expected, and that those are indeed the bytes in the byte stream (sent from another application).
Now, I would be able to understand a bit better if expiration just held some nonsense, because that would mean there's some egregious error (probably involving pointers). But this... this is clearly just spitting out the hex value as if it were a decimal, and that's plain wrong.
To make matters more confusing, this works at another point in the program:
fullSize = ntohs(*(uint16_t*)&buff[0]);
With a byte value of 0x0114, fullSize will contain the value 276.
So the question is, what the heck is going on here? How is it possible for an int to be wrong?
hex is sticky, so unless you reset it, cout will continue to output things in hex.
You can reset it by issuing sending std::dec to the stream. Alternatively you could build a more advanced mechanism that would store the original state and restore it afterwords.
cout << "Expiration: " << dec << (long)expires << endl; will output decimal, otherwise the last setting (hex or dec) will still be in effect.
Since you never switch cout back to decimal output, all of your outputs are in hex, even the output of cout << "Expiration: " << (long)expires << endl;.

c++ hex number format

I'm trying to output the hex value of a char and format it in a nice way.
Required: 0x01 : value 0x1
All I can get is: 00x1 : value 0x1 // or 0x1 if i don't use iomanip
Here's the code i have, 'ch' was declared to be a unsigned char. Is there any other way to do it other than checking the value and manually add an '0'??
cout << showbase;
cout << hex << setw(2) << setfill('0') << (int) ch;
Edit:
I found one solution online:
cout << internal << setw(4) << setfill('0') << hex << (int) ch
std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)ch;
Since setw pads out to the left of the overall printed number (after showbase is applied), showbase isn't usable in your case. Instead, manually print out the base as shown above.
In one on my projects I did this:
ostream &operator<<(ostream &stream, byte value)
{
stream << "0x" << hex << (int)value;
return stream;
}
I surchaged the operator<< for stream output, and anything that was a byte was shown in hexadecimal. byte is a typedef for unsigned char.