473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Dec 7 '07 #1
6 5395
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
Dec 7 '07 #2
Because the standard says it will do so?

Still why?
Dec 7 '07 #3
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
Dec 7 '07 #4
On Dec 7, 5:50 pm, ciccio <no_valid_em...@spam.comwrote:
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();
}

Dec 7 '07 #5
On Dec 7, 8:56 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 7, 5:50 pm, ciccio <no_valid_em...@spam.comwrote:


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;
}
Dec 7 '07 #6
On Dec 7, 4:12 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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:ja*********@gmail.com
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
Dec 7 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jerry | last post by:
Hi there: Does somebody have a solution to this one ? : How can I get a script to send two subsquent pages. Example: My script accepts form data and shows a preliminary "Thank you" page...
2
by: Thomas Covello | last post by:
Hello, When the following perl script is executed: #!/usr/bin/env perl use strict; use diagnostics; use warnings; # Header bytes for different zip formats my $GZIP_HEADER =...
0
by: B4S. | last post by:
Hello, I read many related posts without a working solution for me. I have 2 stations in 2 seperate places that are being used for development. (VS.NET2003 VB.NET aspx web site on a remote...
13
by: Raja Kiran Kumar Reddy Sandireddy | last post by:
Hi, I observed a very strange phenomenon with my program outputs. I run my C-program on MSDOS environment and the same program on Sun-session. I get same output when the input is a small file...
1
by: Greg Geller via .NET 247 | last post by:
Hi all, I have created a web application. I have included a reference to the Microsoft Excel 9.0 object. When I compile the project, I receive the following error: "Could not copy built outputs...
2
by: john sun | last post by:
Hi, Dear gurus, In the web deployment project, there is one option called "Output Assemblies page". The first choice is "Merge all outputs to a single assembly", According to MSDN, "Merges...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
1
by: kcassanova | last post by:
Hi, I've been reading a nice document from Oleg Tkachenko regarding creating multiple XML Output files from one XML file using XSLT. Works good except I have a need for slightly different...
6
by: Taras_96 | last post by:
Hi everyone, The output of echo file_get_contents("http://watchout4snakes.com/creativitytools/ RandomWord/RandomWordPlus.aspx"); leaves the browser empty.. no error messages, nothing. ...
7
TheServant
by: TheServant | last post by:
Hey guys, I have an untraining script in my game which I am having a small problem with. There are 4 levels of soldiers: rec, reg, exp and vet. Now the number of soldier is recorded as the total, so...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.