String to Hex without changing number, C++

Solution 1:

As this is just a matter of different representations of the same value, its IO streams that offer such "conversion":

#include <sstream>
#include <iostream>

int main() {
    std::stringstream ss{"0x42"};
    int x = 0;
    ss >> std::hex >> x;
    std::cout << x << "\n";
    std::cout << std::hex << x;
}

Output:

66
42

Solution 2:

Like this:

#include <string>
#include <iostream>

int main()
{
    std::string s = "0x42";
    int i = std::stoi( s, nullptr, 16 );
    int j = strtoul( s.substr(2).c_str(), nullptr, 16 );
    std::cout << i << "," << j << "\n";
}

Note that std::stoi will handle the leading "0x". strtoul is pickier.