0x0 outputs as 0 instead of 0x0



 DEVELOP > c-Plus-Plus > 0x0 outputs as 0 instead of 0x0

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "ciccio"
Date: 07 Dec 2007 06:50:40 AM
Object: 0x0 outputs as 0 instead of 0x0
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
.

User: "Abhishek Padmanabh"

Title: Re: 0x0 outputs as 0 instead of 0x0 07 Dec 2007 09:56:06 AM
On Dec 7, 5:50 pm, ciccio <no_valid_em...@spam.com> 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::ostream& printHexToStream(std::ostream& os, int input)
std::stringstream ss;
ss << "0x" << std::hex << 26;
os << ss.str();
}
.
User: "Abhishek Padmanabh"

Title: Re: 0x0 outputs as 0 instead of 0x0 07 Dec 2007 10:01:41 AM
On Dec 7, 8:56 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Dec 7, 5:50 pm, ciccio <no_valid_em...@spam.com> 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::ostream& printHexToStream(std::ostream& os, int input)
{
std::stringstream ss;
ss << "0x" << std::hex << input;
os << ss.str();
return os;
}
.


User: "Victor Bazarov"

Title: Re: 0x0 outputs as 0 instead of 0x0 07 Dec 2007 08:32:19 AM
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
.
User: "ciccio"

Title: Re: 0x0 outputs as 0 instead of 0x0 07 Dec 2007 08:56:17 AM

Because the standard says it will do so?

Still why?
.
User: "Victor Bazarov"

Title: Re: 0x0 outputs as 0 instead of 0x0 07 Dec 2007 09:12:03 AM
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
.
User: "James Kanze"

Title: Re: 0x0 outputs as 0 instead of 0x0 07 Dec 2007 10:49:21 AM
On Dec 7, 4:12 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER