0x0 outputs as 0 instead of 0x0

Discussion in 'C++' started by ciccio, Dec 7, 2007.

  1. ciccio

    ciccio Guest

    Hi,

    I was puzzled when i noticed that 'zero' in hexadecimal output is
    written as '0' and not '0x0'. What is the reason of this?

    #include<iostream>

    int main(void) {

    std::cout.setf(std::ios::hex,std::ios::basefield);
    std::cout.setf(std::ios::showbase);

    std::cout << 0 << "\t" << 1 << std::endl;

    return 0;
    }


    output gives
    0 0x1

    Regards
     
    ciccio, Dec 7, 2007
    #1
    1. Advertising

  2. ciccio wrote:
    > I was puzzled when i noticed that 'zero' in hexadecimal output is
    > written as '0' and not '0x0'. What is the reason of this?
    >
    > #include<iostream>
    >
    > int main(void) {
    >
    > std::cout.setf(std::ios::hex,std::ios::basefield);
    > std::cout.setf(std::ios::showbase);
    >
    > std::cout << 0 << "\t" << 1 << std::endl;
    >
    > return 0;
    > }
    >
    >
    > output gives
    > 0 0x1


    Because the standard says it will do so?

    Zero is zero. If you always need "0x" in front of your output you
    should drop the 'showbase' and output "0x" yourself.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 7, 2007
    #2
    1. Advertising

  3. ciccio

    ciccio Guest

    > Because the standard says it will do so?

    Still why?
     
    ciccio, Dec 7, 2007
    #3
  4. ciccio wrote:
    >> Because the standard says it will do so?

    >
    > Still why?


    Not sure. Probably because that's how C's "printf" behaves.
    C++ inherits a lot from C; sometimes the only justification is
    that if folks recompile their code with a C++ compiler they
    don't get any surprises (at least most of the time). Now, C
    doesn't have streams, but there is '#' flag character in the
    'printf' formatting specification. The 'showbase' is defined
    to mimic it.

    If you'd like to know more about justifications that were used
    when designing formatted I/O for C++, ask in 'comp.std.c++',
    they discuss the rationales behind the decisions to put this or
    that in the Standard.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 7, 2007
    #4
  5. On Dec 7, 5:50 pm, ciccio <> wrote:
    > Hi,
    >
    > I was puzzled when i noticed that 'zero' in hexadecimal output is
    > written as '0' and not '0x0'. What is the reason of this?
    >
    > #include<iostream>
    >
    > int main(void) {
    >
    > std::cout.setf(std::ios::hex,std::ios::basefield);
    > std::cout.setf(std::ios::showbase);
    >
    > std::cout << 0 << "\t" << 1 << std::endl;
    >
    > return 0;
    >
    > }
    >
    > output gives
    > 0 0x1
    >


    To keep it consistent, you could use stringstreams and always prefix
    0x:

    std::eek:stream& printHexToStream(std::eek:stream& os, int input)
    std::stringstream ss;
    ss << "0x" << std::hex << 26;
    os << ss.str();
    }
     
    Abhishek Padmanabh, Dec 7, 2007
    #5
  6. On Dec 7, 8:56 pm, Abhishek Padmanabh <>
    wrote:
    > On Dec 7, 5:50 pm, ciccio <> wrote:
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > I was puzzled when i noticed that 'zero' in hexadecimal output is
    > > written as '0' and not '0x0'. What is the reason of this?

    >
    > > #include<iostream>

    >
    > > int main(void) {

    >
    > > std::cout.setf(std::ios::hex,std::ios::basefield);
    > > std::cout.setf(std::ios::showbase);

    >
    > > std::cout << 0 << "\t" << 1 << std::endl;

    >
    > > return 0;

    >
    > > }

    >
    > > output gives
    > > 0 0x1

    >


    Correction:

    To keep it consistent, you could use stringstreams and always prefix
    0x:

    std::eek:stream& printHexToStream(std::eek:stream& os, int input)
    {
    std::stringstream ss;
    ss << "0x" << std::hex << input;
    os << ss.str();
    return os;
    }
     
    Abhishek Padmanabh, Dec 7, 2007
    #6
  7. ciccio

    James Kanze Guest

    On Dec 7, 4:12 pm, "Victor Bazarov" <> wrote:
    > ciccio wrote:
    > >> Because the standard says it will do so?


    > > Still why?


    > Not sure. Probably because that's how C's "printf" behaves.
    > C++ inherits a lot from C; sometimes the only justification is
    > that if folks recompile their code with a C++ compiler they
    > don't get any surprises (at least most of the time). Now, C
    > doesn't have streams, but there is '#' flag character in the
    > 'printf' formatting specification. The 'showbase' is defined
    > to mimic it.


    That's definitely the reason. All C++ says about showbase is
    that if the type is integral and showbase is set, formats "as
    if" there were a # in the specifier to printf. (All of the
    formatting is specified in terms of printf format specifiers.)
    For #, the C standard says: "For x (or X) conversion, a nonzero
    result has 0x (or 0X) prefixed to it."

    The rationale for the C standard doesn't say anything about this
    either.

    (It surprised me, too, just a couple of days ago. In the end,
    however, I wanted something like "0x1AB", with a small x but
    capital hexadecimal digits, so I had to insert the 0x manually
    anyway. And since I was using '0' as a fill character at that
    point, inserting the 0x didn't require any jumping through
    hoops. But I still think it strange.)

    > If you'd like to know more about justifications that were used
    > when designing formatted I/O for C++, ask in 'comp.std.c++',
    > they discuss the rationales behind the decisions to put this
    > or that in the Standard.


    In this case, comp.std.c might be more appropriate. (Of course,
    the answer might be simply that that's what all existing
    implementations did at the time. And who knows why they did
    it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 7, 2007
    #7
    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. E. Backhus
    Replies:
    2
    Views:
    7,085
    Alain
    Jul 25, 2003
  2. Anders Both
    Replies:
    1
    Views:
    11,765
    Alvin Bruney
    Jan 14, 2004
  3. Student
    Replies:
    3
    Views:
    479
    Student
    Aug 22, 2006
  4. Replies:
    1
    Views:
    891
  5. A.M-SG
    Replies:
    3
    Views:
    726
    Steven Cheng[MSFT]
    Dec 2, 2005
Loading...

Share This Page