Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using cout to print hex values; surely you can't be serious

74 views
Skip to first unread message

Chris Stankevitz

unread,
Feb 8, 2012, 12:16:12 AM2/8/12
to
char c = 0xFF;

Q1: How do I use cout to print the variable c so that it appears at
stdout as "FF"?

A1:
#include <iomanip>
#include <iostream>

std::cout << std::hex << std::setfill('0') << std::setw(2) <<
std::uppercase << (static_cast<int>(c) & 0xFF);

Q2: Surely you can't be serious.

A2: I am serious. And don't call me Shirley.

gwowen

unread,
Feb 8, 2012, 6:54:05 AM2/8/12
to
On Feb 8, 5:16 am, Chris Stankevitz <chrisstankev...@gmail.com> wrote:
> char c = 0xFF;
>
> Q1: How do I use cout to print the variable c so that it appears at
> stdout as "FF"?
>
> A1:
> #include <iomanip>
> #include <iostream>
>
>   std::cout << std::hex << std::setfill('0') << std::setw(2) <<
> std::uppercase << (static_cast<int>(c) & 0xFF);

Yeah, brutal isn't it.

Boost::Format provides a printf() type formatting system that isn't
anything like as painful to use.
http://www.boost.org/doc/libs/1_48_0/libs/format/doc/format.html

Victor Bazarov

unread,
Feb 8, 2012, 8:15:30 AM2/8/12
to
On 2/8/2012 12:16 AM, Chris Stankevitz wrote:
> char c = 0xFF;

Have you considered that 0xFF is outside the range a signed 'char' is
guaranteed to represent? Or is your 'char' unsigned by default? Just
checking...

>
> Q1: How do I use cout to print the variable c so that it appears at
> stdout as "FF"?
>
> A1:
> #include<iomanip>
> #include<iostream>
>
> std::cout<< std::hex<< std::setfill('0')<< std::setw(2)<<
> std::uppercase<< (static_cast<int>(c)& 0xFF);
>
> Q2: Surely you can't be serious.
>
> A2: I am serious. And don't call me Shirley.

Similar to "How do I put my trousers on by pulling them over my head?".

Use 'printf', it seems to suit *this particular task* better.

V
--
I do not respond to top-posted replies, please don't ask

Leigh Johnston

unread,
Feb 8, 2012, 9:20:30 AM2/8/12
to
On 08/02/2012 05:16, Chris Stankevitz wrote:
> char c = 0xFF;
>
> Q1: How do I use cout to print the variable c so that it appears at
> stdout as "FF"?
>
> A1:
> #include<iomanip>
> #include<iostream>
>
> std::cout<< std::hex<< std::setfill('0')<< std::setw(2)<<
> std::uppercase<< (static_cast<int>(c)& 0xFF);

That static_cast looks superfluous (result of the expression should be
promoted to 'int' if 'c' is a 'char').

/Leigh

Message has been deleted

gwowen

unread,
Feb 8, 2012, 9:49:16 AM2/8/12
to
On Feb 8, 1:15 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 2/8/2012 12:16 AM, Chris Stankevitz wrote:
>
> Use 'printf', it seems to suit *this particular task* better.

But sometimes you want to mix streaming objects with a custom
operator<< with printing hex values of bytes. Are you suggesting
interleaving

std::cout << complex_object;
printf("%x",byte);

and hope that buffering makes that OK?

Alternatively, you may want to serialise some bytes to a
std::stringstream or serialise some bytes to a stream provided by a
library user as a std::ostream& arguments, or to a file which has been
opened as an std::fstream ... or ... or ...

fprintf is fine, if you're writing to a C-style FILE*.
It's useless for anything else, and there are *plenty* of anything-
elses.

The standard stream-formatting API is an bloody disaster area, so full
of gotchas and weirdness as to be almost-unusable. Boost::Format is
the solution.

Jorgen Grahn

unread,
Feb 8, 2012, 10:20:59 AM2/8/12
to
I tend to use

#include <iostream>
struct format_as_hex {
....
};
std::cout << format_as_hex(c);

and let 'format_as_hex' use sprintf() internally.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Paavo Helde

unread,
Feb 9, 2012, 1:41:51 AM2/9/12
to
Chris Stankevitz <chrisst...@gmail.com> wrote in news:08081227-b09b-
454f-b23d-8...@n12g2000yqb.googlegroups.com:

> char c = 0xFF;
>
> Q1: How do I use cout to print the variable c so that it appears at
> stdout as "FF"?

char buff[3];
sprintf(buff, "%02X", c);
std::cout << buff;

If you are afraid that char is 32 or 64 bits on some platform and might not
fit into the buffer, you can make the buffer e.g. 17 bytes long as a
precaution. The point is that the dreaded buffer overflows associated with
sprintf() can be avoided here easily as the output size is constrained, so
there is no reason why sprintf() could not be used if it suits the task
better.

Cheers
Paavo

Ian Collins

unread,
Feb 9, 2012, 2:22:00 AM2/9/12
to
On 02/ 9/12 07:41 PM, Paavo Helde wrote:
> Chris Stankevitz<chrisst...@gmail.com> wrote in news:08081227-b09b-
> 454f-b23d-8...@n12g2000yqb.googlegroups.com:
>
>> char c = 0xFF;
>>
>> Q1: How do I use cout to print the variable c so that it appears at
>> stdout as "FF"?
>
> char buff[3];
> sprintf(buff, "%02X", c);
> std::cout<< buff;
>
> If you are afraid that char is 32 or 64 bits on some platform and might not
> fit into the buffer, you can make the buffer e.g. 17 bytes long as a
> precaution.

The buffer is an array of char, so it will always be big enough no
matter how big a char is (in c++ terms it is always 1).

> The point is that the dreaded buffer overflows associated with
> sprintf() can be avoided here easily as the output size is constrained, so
> there is no reason why sprintf() could not be used if it suits the task
> better.

Completely agree.

--
Ian Collins

gwowen

unread,
Feb 9, 2012, 3:16:16 AM2/9/12
to
On Feb 9, 7:22 am, Ian Collins <ian-n...@hotmail.com> wrote:
> > If you are afraid that char is 32 or 64 bits on some platform and might not
> > fit into the buffer, you can make the buffer e.g. 17 bytes long as a
> > precaution.
>
> The buffer is an array of char, so it will always be big enough no
> matter how big a char is (in c++ terms it is always 1).

A char value bigger than 255 will need three (or more) char's to hold
its ascii hex representation. No matter how big a char is, once
you're representing it as an ASCII sequence of 0-9A-F, you're going to
need a string of length ~ log_16(CHAR_MAX). Just like you need 3
bytes to hold the ascii decimal representation of an 8-bit char now, "
0"-"255"

Ian Collins

unread,
Feb 9, 2012, 3:44:47 AM2/9/12
to
Don't forget the the format string was "%02X"..

--
Ian Collins

Juha Nieminen

unread,
Feb 9, 2012, 5:36:01 AM2/9/12
to
Ian Collins <ian-...@hotmail.com> wrote:
> Don't forget the the format string was "%02X"..

That defines the minimum amount of characters to output, not the maximum.

gwowen

unread,
Feb 9, 2012, 6:56:04 AM2/9/12
to
On Feb 9, 8:44 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Don't forget the the format string was "%02X"..

Don't forget that width specifiers are a minumum. (Another talented,
concientious, experienced C-user, and *boom* another potential
sprintf() overflow bug).

sprintf() is horrid.

Scott Lurndal

unread,
Feb 9, 2012, 12:04:42 PM2/9/12
to
Buffer overflows can be avoided completely with snprintf. sprintf should
be considered deprecated.

scott

Scott Lurndal

unread,
Feb 9, 2012, 12:06:18 PM2/9/12
to
gwowen <gwo...@gmail.com> writes:
Which is why snprintf(3c) is preferred.

scott

Jorgen Grahn

unread,
Feb 12, 2012, 10:59:40 AM2/12/12
to
You still have to decide though what to do when you have truncation.
If you ignore the error and continue that might (in some cases) be
just as bad as the sprintf() buffer overflow.

io_x

unread,
Feb 19, 2012, 12:24:19 PM2/19/12
to

"gwowen" <gwo...@gmail.com> ha scritto nel messaggio
news:1b7b2943-4c64-4737...@db5g2000vbb.googlegroups.com...
On Feb 8, 1:15 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 2/8/2012 12:16 AM, Chris Stankevitz wrote:
>
> Use 'printf', it seems to suit *this particular task* better.

But sometimes you want to mix streaming objects with a custom
operator<< with printing hex values of bytes. Are you suggesting
interleaving

std::cout << complex_object;
printf("%x",byte);

and hope that buffering makes that OK?

#io_x
#yes if cout and printf use the same stream and the same stream buffer

88888 Dihedral

unread,
Feb 20, 2012, 6:19:35 AM2/20/12
to
在 2012年2月20日星期一UTC+8上午1时24分19秒,io_x写道:
> "gwowen" <gwo...@gmail.com> ha scritto nel messaggio
> news:1b7b2943-4c64-4737...@db5g2000vbb.googlegroups.com...
> On Feb 8, 1:15 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> > On 2/8/2012 12:16 AM, Chris Stankevitz wrote:
> >
> > Use 'printf', it seems to suit *this particular task* better.
>
> But sometimes you want to mix streaming objects with a custom
> operator<< with printing hex values of bytes. Are you suggesting
> interleaving

Well, after my carefully investigations of << and >>
, I bielive it is not so trivail in C++ to control.

For example, I have to set up bufers for a
data compressed encoder and decoder of various comprssion methods with options for fast speed RW operations or smaller sizes in the heap or the swap
files in the hd.

How do I pass the option?

Unless I design a control foarmt for the stream
or use some external methods to pass the options
to the stream operators, I can't think of better ways
in C++ stream operator reloading.

I am wondering is there other way for the >> and <<
operator reloadings.

88888 Dihedral

unread,
Feb 21, 2012, 6:08:37 AM2/21/12
to
88888 Dihedral於 2012年2月20日星期一UTC+8下午7時19分35秒寫道:
I studied the python bit-torrent long time ago. It is easy to set up sites
to join the torrents in Python. But how to do that in C++??
0 new messages