std::hex
causes integers to be printed in hexadecimal representation. In order to revert back to hexadecimal representation, std::dec
is used. // // g++ hex.cpp // #include <iostream> #include <iomanip> int main() { std::cout << 100 << std::endl; std::cout << std::hex << 100 << std::endl; std::cout << 100.1 << std::endl; std::cout << std::dec << 100 << std::endl; }
100 64 100.1 100
std::hex
can also be used to convert a string that contains an integral value in hexadecimal represantion to a long
: #include <iostream> #include <sstream> #include <string> int main() { std::stringstream ss; std::string value_in_hex("1E240"); long value_in_dec; ss << std::hex << "1E240"; ss >> value_in_dec; std::cout << "0x" << value_in_hex << " = " << value_in_dec << std::endl; }