Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 0x0 outputs as 0 instead of 0x0

Reply
Thread Tools

0x0 outputs as 0 instead of 0x0

 
 
ciccio
Guest
Posts: n/a
 
      12-07-2007
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
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


 
Reply With Quote
 
 
 
 
ciccio
Guest
Posts: n/a
 
      12-07-2007
> Because the standard says it will do so?

Still why?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
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


 
Reply With Quote
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 5:50 pm, ciccio <(E-Mail Removed)> 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:stream& printHexToStream(std:stream& os, int input)
std::stringstream ss;
ss << "0x" << std::hex << 26;
os << ss.str();
}

 
Reply With Quote
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 8:56 pm, Abhishek Padmanabh <(E-Mail Removed)>
wrote:
> On Dec 7, 5:50 pm, ciccio <(E-Mail Removed)> 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:stream& printHexToStream(std:stream& os, int input)
{
std::stringstream ss;
ss << "0x" << std::hex << input;
os << ss.str();
return os;
}
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 4:12 pm, "Victor Bazarov" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tools.o(.data+0x0): multiple definition of `VAR_1' Student C Programming 3 08-22-2006 08:06 AM
The thread '<No Name>' (0xdc0) has exited with code 0 (0x0). A.M-SG ASP .Net Web Services 3 12-02-2005 11:48 AM
The thread {----} has exited with code 0 (0x0). chuayongquan@hotmail.com C++ 1 07-05-2005 01:11 PM
Re: PIX-FLASH-16MB - can't get it to work : Serial number: 0 (0x0) Nolajc2000 Cisco 2 04-01-2004 06:40 AM
The thread '<No Name>' (0x974) has exited with code 0 (0x0) ? Anders Both ASP .Net 1 01-14-2004 07:51 PM



Advertisments