Search notes:

C++ Standard Library: std::hex

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;

}
Github repository about-cpp-standard-library, path: /iomanip/hex.cpp
Output:
100
64
100.1
100

Converting from hexadecimal representation to long

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;
}
Github repository about-cpp-standard-library, path: /iomanip/hex_convert-hex-to-dec.cpp

See also

C++ Standard Library

Index