My teacher says that for the assignment we are not allowed to manually print the 0x before the hex number, we have to make the system do it.
Currently my code looks like this:
cout << "Hex" << setw(12) << hex << static_cast<int>(letter) << setw(12) << hex
<< number << setw(12) << hex << static_cast<int> (symbol) << endl;
It prints the correct hex value but without the 0x.
Additionally, for octal numbers, I have to again, make the system print a 0 before the number (not manually. My code prints correct values, but without the preceding 0:
cout << "Octal" << setw(12) << setbase(8) << static_cast<int>(letter) << setw(12) << setbase(8)
<< number << setw(12) << setbase(8) << static_cast<int>(symbol) << endl;
The prefix we use for hexadecimal is "0x". To represent the numbers 0-9, we simply use those digits. To represent 10-15, we use the letters A-F.
To specify your constant in hex, use a leading 0x , e.g. 0xf . The same thing applies in any other context that can take a (decimal) value - you get hex by using a leading 0x . Or if you want octal for some reason, use a leading 0 , without the x.
If you print a hexadecimal number, Python uses the prefix '0x' to indicate that it's a number in the hexadecimal system and not in the decimal system like normal integers.
Use std::showbase
:
std::cout << std::hex << std::showbase << 1234567890;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With