CKTIS / C++
std::hex — hexadecimal integer output and input
std::hex selects base 16 for formatted integer output and input on a stream. The setting remains active until changed; it changes neither the numeric value nor the formatting of floating-point numbers.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <ios>
out << std::hex << value;
in >> std::hex >> value;std::hex is a stream manipulator declared in <ios>. Use out << std::hex << value to write an integer in hexadecimal, or in >> std::hex >> value to read it in base 16. The manipulator changes conversion between numbers and text, not the numeric value itself.
Its signature is std::ios_base& std::hex(std::ios_base& str): it calls str.setf(std::ios_base::hex, std::ios_base::basefield) and returns str. std::hex is a function, whereas std::ios_base::hex is a flag. The setting replaces the previous base selection and remains active on that stream for subsequent operations. std::dec restores base 10, and std::oct selects base 8.
std::hex alone does not enable the 0x prefix; use std::showbase for that. std::uppercase controls uppercase letters in hexadecimal output. These flags are independent of the base. std::hex does not select hexadecimal floating-point formatting — use std::hexfloat for that.
The example writes 255 and 16 as ff and 10, then returns to decimal output. A separate stream reads ff as the value 255. Settings on one stream do not change the settings of other streams.
Examples
#include <ios>
#include <iostream>
#include <sstream>
int main() {
std::ostringstream out;
out << std::hex << 255 << ' ' << 16;
out << ' ' << std::dec << 255;
std::istringstream in("ff");
int value = 0;
if (!(in >> std::hex >> value)) {
return 1;
}
std::cout << out.str() << '\n' << value << '\n';
}
Result
ff 10 255 255
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.