zeulb zeulb - 2 months ago 23
C++ Question

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.

Answer
#include <iostream>
#include <iomanip>

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

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

    return 0;
}
Comments