-1
inpfile>>ch;

if(ch<16) outfile<<"0×0"<<std::hex<<setprecision(2)<<(int)ch<<" ";

what does std::hex<<setprecision(2) mean?

COMer
  • 1,117
  • 4
  • 14
  • 24
  • 3
    Google is your friend: http://www.cplusplus.com/reference/iostream/manipulators/hex/ http://www.cplusplus.com/reference/iostream/manipulators/setprecision/ – user229044 Sep 20 '10 at 16:26
  • @meagar: IIUC, the goal of SO is to be what google points to when someone asks a programming-related question. For that to work, these questions have to be asked and answered here first. – sbi Sep 20 '10 at 16:29
  • 1
    @sbi There has to be some cut off where the questions become too close to "will you do my thinking for me?", and in my opinion this question skirts that line. SO should be the place Google points for *worth-while* questions, not simple questions best answered by the documentation. Otherwise we might as well just mirror the documentation and be done with it. – user229044 Sep 21 '10 at 14:47
  • @meagar. I think it's legitimate to ask what `std::hex` and `std::setprecision()` are for. Granted, the question is worded badly, but 1) English is not everyone's first language and 2) you have enough reputation to improve it. – sbi Sep 21 '10 at 14:50
  • @sbi I disagree. The slightest amount of research would have revealed the existence and purpose of stream manipulators. I don't consider it the job of SO to mirror the documentation in question/answer form. People need to be encouraged to figure this stuff out first hand rather than Googling it vicariously through SO. – user229044 Sep 21 '10 at 14:52
  • @meagar: ...which brings us around to my first comment. I guess we just have to agree to disagree then. – sbi Sep 21 '10 at 16:16
  • @sbi How does that bring us back around? Are you saying that SO *should* be the first result on Google for "std::hex"? What about "php array_first"? Should we mirror the documentation in question/answer form so that we rank higher than PHP's own documentation? At some point questions aren't questions, they're roundabout ways of asking us to regurgitate the existing documentation. "What is std::hex?" is such a question. – user229044 Sep 21 '10 at 18:13

4 Answers4

2

iostreams 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

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
1

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.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
1

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')

Martin York
  • 257,169
  • 86
  • 333
  • 562
0

std::hex sets the output base to hexadecimal.

setprecision has no effect on this line since it affects floating point only.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220