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.
  • 59,901
  • 24
  • 121
  • 211
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
  • 216,937
  • 30
  • 383
  • 461
3

Don't use the std::hex manipulator?

std::cout << int(x) << std::endl;
Some programmer dude
  • 380,411
  • 33
  • 383
  • 585