C++: Converting Hexadecimal to Decimal

I'm looking for a way to convert hex(hexadecimal) to dec(decimal) easily. I found an easy way to do this like :

int k = 0x265;
cout << k << endl;

But with that I can't input 265. Is there anyway for it to work like that:

Input: 265

Output: 613

Is there anyway to do that ?

Note: I've tried:

int k = 0x, b;
cin >> b;
cout << k + b << endl;

and it doesn't work.


Solution 1:

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
    int x, y;
    std::stringstream stream;

    std::cin >> x;
    stream << x;
    stream >> std::hex >> y;
    std::cout << y;

    return 0;
}

Solution 2:

Use std::hex manipulator:

#include <iostream>
#include <iomanip>

int main()
{
    int x;
    std::cin >> std::hex >> x;
    std::cout << x << std::endl;

    return 0;
}