It is used to 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.
Following is the declaration for std::hex function.
ios_base& hex (ios_base& str);
str − Stream object whose format flag is affected.
It returns Argument str.
Basic guarantee − if an exception is thrown, str is in a valid state.
It modifies str. Concurrent access to the same stream object may cause data races.
In below example explains about std::hex function.
#include <iostream> 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; }
Let us compile and run the above program, this will produce the following result −
70 46 106