c++hexdecimalbuilt-in

Decimal to hex conversion c++ built-in function


Is there a built-in function in c++ that would take a decimal input from a user and convert it to hex and vice versa?? I have tried it using a function I've written but I was wondering if there is a built-in one to minimize the code a little bit. Thanks in advance.


Solution

  • Decimal to hex :-

    #include <sstream>
    
    std::stringstream ss;
    ss << std::hex << decimal_value; // int decimal_value
    std::string res ( ss.str() );
    
    std::cout << res;
    

    Hex to decimal :-

    #include <sstream>
    
    std::stringstream ss;
    ss << hex_value; // std::string hex_value
    ss >> std::hex >> decimal_value; //int decimal_value
    
    std::cout << decimal_value;
    

    Ref: std::hex, std::stringstream