inpfile>>ch;
if(ch<16) outfile<<"0×0"<<std::hex<<setprecision(2)<<(int)ch<<" ";
what does std::hex<<setprecision(2)
mean?
inpfile>>ch;
if(ch<16) outfile<<"0×0"<<std::hex<<setprecision(2)<<(int)ch<<" ";
what does std::hex<<setprecision(2)
mean?
iostream
s can be manipulated to achieve the desired formatting - this is done by what at first sight looks like outputting predefined values to them as shown in our subject line of code.
std::hex
displays following integer values in base16.
setprecision
sets the precision for display of following floating values.
Fur further info on manipulators, start here
what does
std::hex<<setprecision(2)
mean?
std::hex
and std::setprecision
are both so-called manipulators. Applied to a stream (done by outputting them) they manipulate the stream, usually to change the stream's formatting. In particular, std::hex
manipulates the stream so that values are written in hexadecimal, and std::setprecision(x)
manipulates it to output numbers with x
digits.
(A rather popular manipulator which you might already know about is std::endl
.)
As you can see, there are manipulators that take arguments and those that take none. Also, most (in principle all) manipulators are sticky, which means their manipulation of the stream lasts until it is explicitly changed. Here is an extensive discussion about this topic.
This line is the same as:
char ch;
inpfile>>ch;
if(ch<16)
{
outfile << "0×0" // Prints "0x0" (Ox is the standard prefix for hex numbers)
/*outfile*/ << std::hex // Tells the stream to print the next number in hex format
/*outfile*/ << setprecision(2) // Does nothing. Presumably they were trying to indicate print min of 2 characters
/*outfile*/ << (int)ch // Covert you char to an integer (so the stream will print it in hex
/*outfile*/ << " "; // Add a space for good measure.
}
Rather than setprecision(2) what was probably intended was setw(2) << setfill('0')
std::hex
sets the output base to hexadecimal.
setprecision
has no effect on this line since it affects floating point only.