Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13834 > unrolled thread

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

Started byChris Stankevitz <chrisstankevitz@gmail.com>
First post2012-02-07 21:16 -0800
Last post2012-02-09 17:04 +0000
Articles 18 — 11 participants

Back to article view | Back to comp.lang.c++


Contents

  Using cout to print hex values; surely you can't be serious Chris Stankevitz <chrisstankevitz@gmail.com> - 2012-02-07 21:16 -0800
    Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-08 03:54 -0800
    Re: Using cout to print hex values; surely you can't be serious Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-08 08:15 -0500
      Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-08 06:49 -0800
        Re: Using cout to print hex values; surely you can't be serious "io_x" <a@b.c.invalid> - 2012-02-19 18:24 +0100
          Re: Using cout to print hex values; surely you can't be serious 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-20 03:19 -0800
            Re: Using cout to print hex values; surely you can't be serious 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-21 03:08 -0800
      Re: Using cout to print hex values; surely you can't be serious Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-08 15:20 +0000
    Re: Using cout to print hex values; surely you can't be serious Leigh Johnston <leigh@i42.co.uk> - 2012-02-08 14:20 +0000
    Re: Using cout to print hex values; surely you can't be serious Paavo Helde <myfirstname@osa.pri.ee> - 2012-02-09 00:41 -0600
      Re: Using cout to print hex values; surely you can't be serious Ian Collins <ian-news@hotmail.com> - 2012-02-09 20:22 +1300
        Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-09 00:16 -0800
          Re: Using cout to print hex values; surely you can't be serious Ian Collins <ian-news@hotmail.com> - 2012-02-09 21:44 +1300
            Re: Using cout to print hex values; surely you can't be serious Juha Nieminen <nospam@thanks.invalid> - 2012-02-09 10:36 +0000
            Re: Using cout to print hex values; surely you can't be serious gwowen <gwowen@gmail.com> - 2012-02-09 03:56 -0800
              Re: Using cout to print hex values; surely you can't be serious scott@slp53.sl.home (Scott Lurndal) - 2012-02-09 17:06 +0000
                Re: Using cout to print hex values; surely you can't be serious Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-12 15:59 +0000
      Re: Using cout to print hex values; surely you can't be serious scott@slp53.sl.home (Scott Lurndal) - 2012-02-09 17:04 +0000

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

FromChris Stankevitz <chrisstankevitz@gmail.com>
Date2012-02-07 21:16 -0800
SubjectUsing cout to print hex values; surely you can't be serious
Message-ID<08081227-b09b-454f-b23d-85ebb98207c0@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"?

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.

[toc] | [next] | [standalone]


#13844

Fromgwowen <gwowen@gmail.com>
Date2012-02-08 03:54 -0800
Message-ID<c16ff914-4684-4eb8-9405-d681128e225d@dn8g2000vbb.googlegroups.com>
In reply to#13834
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

[toc] | [prev] | [next] | [standalone]


#13846

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2012-02-08 08:15 -0500
Message-ID<jgtsi8$5mb$1@dont-email.me>
In reply to#13834
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

[toc] | [prev] | [next] | [standalone]


#13851

Fromgwowen <gwowen@gmail.com>
Date2012-02-08 06:49 -0800
Message-ID<1b7b2943-4c64-4737-be4b-a20c495148ad@db5g2000vbb.googlegroups.com>
In reply to#13846
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.

[toc] | [prev] | [next] | [standalone]


#14290

From"io_x" <a@b.c.invalid>
Date2012-02-19 18:24 +0100
Message-ID<4f41306e$1$1383$4fafbaef@reader1.news.tin.it>
In reply to#13851
"gwowen" <gwowen@gmail.com> ha scritto nel messaggio
news:1b7b2943-4c64-4737-be4b-a20c495148ad@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

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.


[toc] | [prev] | [next] | [standalone]


#14320

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-02-20 03:19 -0800
Message-ID<3872944.347.1329736775505.JavaMail.geo-discussion-forums@pbjr9>
In reply to#14290
在 2012年2月20日星期一UTC+8上午1时24分19秒,io_x写道:
> "gwowen" <gwowen@gmail.com> ha scritto nel messaggio
> news:1b7b2943-4c64-4737-be4b-a20c495148ad@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.

  


> 
> 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
> 
> 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.

[toc] | [prev] | [next] | [standalone]


#14365

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-02-21 03:08 -0800
Message-ID<30431258.1671.1329822517590.JavaMail.geo-discussion-forums@pbgq3>
In reply to#14320
88888 Dihedral於 2012年2月20日星期一UTC+8下午7時19分35秒寫道:
> 在 2012年2月20日星期一UTC+8上午1时24分19秒,io_x写道:
> > "gwowen" <gwowen@gmail.com> ha scritto nel messaggio
> > news:1b7b2943-4c64-4737-be4b-a20c495148ad@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.
> 
>   
> 
> 
> > 
> > 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
> > 
> > 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.

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++??

[toc] | [prev] | [next] | [standalone]


#13853

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-02-08 15:20 +0000
Message-ID<slrnjj54mp.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#13846
On Wed, 2012-02-08, Victor Bazarov wrote:
> 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.

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   .

[toc] | [prev] | [next] | [standalone]


#13848

FromLeigh Johnston <leigh@i42.co.uk>
Date2012-02-08 14:20 +0000
Message-ID<hbednRaXJuoyGa_SnZ2dnUVZ8r6dnZ2d@giganews.com>
In reply to#13834
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

[toc] | [prev] | [next] | [standalone]


#13862

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2012-02-09 00:41 -0600
Message-ID<Xns9FF44420CAC16myfirstnameosapriee@216.196.109.131>
In reply to#13834
Chris Stankevitz <chrisstankevitz@gmail.com> wrote in news:08081227-b09b-
454f-b23d-85ebb98207c0@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

[toc] | [prev] | [next] | [standalone]


#13863

FromIan Collins <ian-news@hotmail.com>
Date2012-02-09 20:22 +1300
Message-ID<9phagoFom1U7@mid.individual.net>
In reply to#13862
On 02/ 9/12 07:41 PM, Paavo Helde wrote:
> Chris Stankevitz<chrisstankevitz@gmail.com>  wrote in news:08081227-b09b-
> 454f-b23d-85ebb98207c0@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

[toc] | [prev] | [next] | [standalone]


#13864

Fromgwowen <gwowen@gmail.com>
Date2012-02-09 00:16 -0800
Message-ID<0684f226-3bfc-46a7-8581-ce345da2be4e@b18g2000vbz.googlegroups.com>
In reply to#13863
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"

[toc] | [prev] | [next] | [standalone]


#13866

FromIan Collins <ian-news@hotmail.com>
Date2012-02-09 21:44 +1300
Message-ID<9phfbvF8slU1@mid.individual.net>
In reply to#13864
On 02/ 9/12 09:16 PM, gwowen wrote:
> 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"

Don't forget the the format string was "%02X"..

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#13869

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-09 10:36 +0000
Message-ID<4f33a191$0$2776$7b1e8fa0@news.nbl.fi>
In reply to#13866
Ian Collins <ian-news@hotmail.com> wrote:
> Don't forget the the format string was "%02X"..

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

[toc] | [prev] | [next] | [standalone]


#13870

Fromgwowen <gwowen@gmail.com>
Date2012-02-09 03:56 -0800
Message-ID<095c9d03-a205-42c8-b4c4-f6f1964ec745@y10g2000vbn.googlegroups.com>
In reply to#13866
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.

[toc] | [prev] | [next] | [standalone]


#13872

Fromscott@slp53.sl.home (Scott Lurndal)
Date2012-02-09 17:06 +0000
Message-ID<e2TYq.115370$241.72809@news.usenetserver.com>
In reply to#13870
gwowen <gwowen@gmail.com> writes:
>On Feb 9, 8:44=A0am, 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.

Which is why snprintf(3c) is preferred.  

scott

[toc] | [prev] | [next] | [standalone]


#13940

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-02-12 15:59 +0000
Message-ID<slrnjjfofa.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#13872
On Thu, 2012-02-09, Scott Lurndal wrote:
> gwowen <gwowen@gmail.com> writes:
>>On Feb 9, 8:44=A0am, 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.
>
> Which is why snprintf(3c) is preferred.  

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.

/Jorgen

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

[toc] | [prev] | [next] | [standalone]


#13871

Fromscott@slp53.sl.home (Scott Lurndal)
Date2012-02-09 17:04 +0000
Message-ID<K0TYq.115369$241.28232@news.usenetserver.com>
In reply to#13862
Paavo Helde <myfirstname@osa.pri.ee> writes:
>Chris Stankevitz <chrisstankevitz@gmail.com> wrote in news:08081227-b09b-
>454f-b23d-85ebb98207c0@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.

Buffer overflows can be avoided completely with snprintf.   sprintf should
be considered deprecated.

scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web