
In article <7253f6b30801300934j29aa2c09ib5c07256a50a38ff@mail.gmail.com>, Chun ping wang wrote:
I think the first one won't work because %d is for decimal printing , and char is a character.
Well ... yes, that's right. char is a character, and what I want to do is to print its decimal value. We're using a char here as an 8-bit int not to hold a character.
Format is a typesafe library so the document should say any type safe format will work in here as in printf.
char to int is a safe conversion ... the documentation leads me to expect that it will work in format the same way as it does in printf, but it doesn't. The question is: is the code wrong, or is the documentation misleading?
Instead this would. printf( "Using printf: ii=%d, cc=%d\n", ii, cc ); cout << boost::format( "Using boost::format: ii=%1d%, cc=%2d%" ) % ii % cc << endl;
No: %2d$ gives exactly the same as %2% or %d. The only workaround I've found is to cast the char variable to int. Cheers, Daniel