C++: Converting Hexadecimal to Decimal

algorithm, c++, decimal, hex

Solution

#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;
}

Problem

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.

Original source

Related problems