function
<ios> <iostream>

std::hex

ios_base& hex (ios_base& str);
Use hexadecimal base
Sets the basefield format flag for the str stream to hex.

When basefield is set to hex, integer values inserted into the stream are expressed in hexadecimal base (i.e., radix 16). For input streams, extracted values are also expected to be expressed in hexadecimal base when this flag is set.

The basefield format flag can take any of the following values (each with its own manipulator):

flag valueeffect when set
decread/write integer values using decimal base format.
hexread/write integer values using hexadecimal base format.
octread/write integer values using octal base format.

Notice that the basefield flag only affects the insertion/extraction of integer values (floating-point values are always interpreted in decimal base).

Notice also that no base prefix is implicitly prepended to the number unless the showbase format flag is set.

For standard streams, the basefield flag is set to dec on initialization.

Parameters

str
Stream object whose basefield format flag is affected.
Because this function is a manipulator, it is designed to be used alone with no arguments in conjunction with the insertion (<<) and extraction (>>) operations on streams (see example below).

Return Value

Argument str.

Example

1
2
3
4
5
6
7
8
9
10
// modify basefield
#include <iostream>     // std::cout, std::dec, std::hex, std::oct

int main () {
  int n = 70;
  std::cout << std::dec << n << '\n';
  std::cout << std::hex << n << '\n';
  std::cout << std::oct << n << '\n';
  return 0;
}

Output:
70
46
106


Data races

Modifies str. Concurrent access to the same stream object may cause data races.

Exception safety

Basic guarantee: if an exception is thrown, str is in a valid state.

See also