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

Discussion in 'C++' started by Chris Stankevitz, Feb 8, 2012.

  1. 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.
     
    Chris Stankevitz, Feb 8, 2012
    #1
    1. Advertising

  2. Chris Stankevitz

    gwowen Guest

    On Feb 8, 5:16 am, 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);


    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
     
    gwowen, Feb 8, 2012
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Feb 8, 2012
    #3
  4. Chris Stankevitz

    Stefan Ram Guest

    Chris Stankevitz <> writes:
    >Q1: How do I use cout to print the variable c so that it appears at
    >stdout as "FF"?
    > std::cout << std::hex << std::setfill('0') << std::setw(2) <<
    >std::uppercase << (static_cast<int>(c) & 0xFF);


    This is not guaranteed to print »FF« as required.

    Maybe you wanted to print a hexadecimal representation of
    the value of c as »FF«?

    { assert c == 0xff; ::std::cout << "FF"; }
     
    Stefan Ram, Feb 8, 2012
    #4
  5. Chris Stankevitz

    gwowen Guest

    On Feb 8, 1:15 pm, Victor Bazarov <> 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::eek:stream& 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.
     
    gwowen, Feb 8, 2012
    #5
  6. Chris Stankevitz

    Jorgen Grahn Guest

    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 .
     
    Jorgen Grahn, Feb 8, 2012
    #6
  7. Chris Stankevitz

    Ian Collins Guest

    On 02/ 9/12 07:41 PM, Paavo Helde wrote:
    > Chris Stankevitz<> wrote in news:08081227-b09b-
    > :
    >
    >> 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
     
    Ian Collins, Feb 9, 2012
    #7
  8. Chris Stankevitz

    gwowen Guest

    On Feb 9, 7:22 am, Ian Collins <> wrote:
    > > If you are afraid that char is 32 or 64 bits on some platform and mightnot
    > > 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"
     
    gwowen, Feb 9, 2012
    #8
  9. Chris Stankevitz

    Ian Collins Guest

    On 02/ 9/12 09:16 PM, gwowen wrote:
    > On Feb 9, 7:22 am, Ian Collins<> 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
     
    Ian Collins, Feb 9, 2012
    #9
  10. Ian Collins <> wrote:
    > Don't forget the the format string was "%02X"..


    That defines the minimum amount of characters to output, not the maximum.
     
    Juha Nieminen, Feb 9, 2012
    #10
  11. Chris Stankevitz

    gwowen Guest

    On Feb 9, 8:44 am, Ian Collins <> 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.
     
    gwowen, Feb 9, 2012
    #11
  12. Chris Stankevitz

    Jorgen Grahn Guest

    On Thu, 2012-02-09, Scott Lurndal wrote:
    > gwowen <> writes:
    >>On Feb 9, 8:44=A0am, Ian Collins <> 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 .
     
    Jorgen Grahn, Feb 12, 2012
    #12
  13. 在 2012å¹´2月20日星期一UTC+8上åˆ1æ—¶24分19秒,io_x写é“:
    > "gwowen" <> ha scritto nel messaggio
    > news:...
    > On Feb 8, 1:15 pm, Victor Bazarov <> 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::eek:stream& 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.
     
    88888 Dihedral, Feb 20, 2012
    #13
  14. 88888 Dihedralæ–¼ 2012å¹´2月20日星期一UTC+8下åˆ7時19分35秒寫é“:
    > 在 2012å¹´2月20日星期一UTC+8上åˆ1æ—¶24分19秒,io_x写é“:
    > > "gwowen" <> ha scritto nel messaggio
    > > news:....
    > > On Feb 8, 1:15 pm, Victor Bazarov <> 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::eek:stream& 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++??
     
    88888 Dihedral, Feb 21, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Dixson
    Replies:
    1
    Views:
    522
    Joe Fallon
    May 18, 2004
  2. Replies:
    10
    Views:
    6,278
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    488
    Juha Autero
    Aug 19, 2003
  4. Generic Usenet Account

    "<< hex" vs cout.setf(ios_base::hex)

    Generic Usenet Account, Jul 11, 2005, in forum: C++
    Replies:
    2
    Views:
    381
    upashu2
    Jul 12, 2005
  5. Replies:
    4
    Views:
    327
    Howard
    Sep 15, 2005
Loading...

Share This Page