Formatting in C++

Jing Su jingsu-26n5VD7DAF2Tm46uYYfjYg at public.gmane.org
Fri Dec 26 02:41:16 UTC 2003


> > #include<iomanip>
> >
> > cout.setf(std::ios::fixed | std::ios::hex);
> > cout << std::setprecision(2) << buf[i];
> I tried this before; it compiles without any complaint, but gives garbage
> output.
> I tried Google, but no relevent info.
> John

I tried a short example, which seemed to work... snippet below.  The only
catch I found was I had to cast the number.  I think the operator
overload is sending the number into the character interpretor instead of
the numeric interpretor.

std::cout << setw(2) << (unsigned int)std::hex << buf[i];

that seemed to do the trick.  Shows the number as hex, space filling to
right align the output at two spaces.
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list