Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / March 2008



Tip: Looking for answers? Try searching our database.

One more C++ cout question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Can I Get a Word In - 22 Mar 2008 08:00 GMT
I have a hex value, 0x14000, and I want to output it as, 0x00014000. Is
it possible to get this output using std::cout?
Thanks
Gregory Weston - 22 Mar 2008 10:06 GMT
> I have a hex value, 0x14000, and I want to output it as, 0x00014000. Is
> it possible to get this output using std::cout?
> Thanks

#include <iostream>
using namespace std;
int main(int argc, const char* argv[])
{
cout << "0x";
cout.flags(ios::hex);
cout.width(8);
cout.fill('0');
cout << 0x14000 << "\n";
return 0;
}
David Phillip Oster - 22 Mar 2008 16:28 GMT
> > I have a hex value, 0x14000, and I want to output it as, 0x00014000. Is
> > it possible to get this output using std::cout?
[quoted text clipped - 11 lines]
> return 0;
> }

That works, but I prefer to use io manipulators:

#include <iostream>
#include <iomanip>
using std::cout;
using std::setw;
using std::setfill;
using std::ios;
using std::hex;
using std::dec;

int main(int argc, char * const argv[]) {

 cout << " 0x" << setw(8) << setfill('0')  << hex << 0x14000 << "\n" << dec << 123 << "\n";
 
 return 0;
}
Gregory Weston - 22 Mar 2008 19:49 GMT
> > > I have a hex value, 0x14000, and I want to output it as, 0x00014000. Is
> > > it possible to get this output using std::cout?
[quoted text clipped - 30 lines]
>   return 0;
> }

Certainly a more elegant solution, but I figured the question was basic
enough that it kind of warranted the most explicit/obvious possible
answer. I've found manipulators confuse people completely new to the
language more than they help right up front.
Can I Get a Word In - 24 Mar 2008 00:04 GMT
>>>> I have a hex value, 0x14000, and I want to output it as, 0x00014000. Is
>>>> it possible to get this output using std::cout?
[quoted text clipped - 33 lines]
> answer. I've found manipulators confuse people completely new to the
> language more than they help right up front.
Thanks for the reply. I'm actually comfortable using manipulators, it
just wasn't immediately obvious how to use them to get the output I wanted.
Can I Get a Word In - 24 Mar 2008 00:05 GMT
>>> I have a hex value, 0x14000, and I want to output it as, 0x00014000. Is
>>> it possible to get this output using std::cout?
[quoted text clipped - 28 lines]
>   return 0;
> }
Thanks, I got it.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.