0

Convert to hex:

cout << hex << int(x) << endl;

How to convert conversely, from hex to dec?

Enter hex number simple:

cin >> hex >> x;
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user2605734
  • 1
  • 1
  • 2
  • 1
    Integers are printed as decimal by default. Could be your problem is actually _inputting_ hexadecimal numbers? I.e. as in this question: http://stackoverflow.com/questions/11031159/converting-hexadecimal-to-decimal – jogojapan Jul 22 '13 at 06:25

2 Answers2

5

You can use the std::dec IO manipulator:

std::cout << std::dec << int(x) << endl;

Note that this is only necessary if you have previously used std::hex or other means to manipulate the base of std::cout. Otherwise you need take no action: the default for an int is decimal.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

Don't use the std::hex manipulator?

std::cout << int(x) << std::endl;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621