Sign in to follow this  

c++: need help converting a decimal number to hex

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Thanks, however, it just seems to print the LSD (least significant digit) instead of the full hex representation aka 0x00000001. Instead it's only 1. Any way around this?

Share this post


Link to post
Share on other sites
Yep it worked. Now it's just not showing 0x before the numbers. I could use cout to put it in, but if any better way, please let me know. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by philvaira
I'm leaving this unhelpful site.


Actually, IIRC cout provides a mechanism to print the preceding 0x, but I can't remember exactly what it was...



[Edited by - evolutional on September 14, 2004 4:28:53 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Hare
Actually, IIRC cout provides a mechanism to print the preceding 0x, but I can't remember exactly what it was...

Luckily, "The C++ Programming Language" by Bjarne Stroustrup can!

To set hex output, either (1) use the strmanip function hex, or (2) set the hex flag.

(1)

#include <iostream>

void main()
{
int a=16;
std::cout << std::hex << a << std::endl;
}




(2)

#include <iostream>

void main()
{
int a=16;
std::cout.setf(std::ios::hex, std::ios::basefield); //basefield specifies the base flag to be set
std::cout << a << std::endl;
}




To display '0x', or whatever prefix is required for the base, set the showbase flag using either of the two previous methods...
(1)

#include <iostream>

void main()
{
int a=16;
std::cout << std::hex << std::showbase << a << std::endl;
}




(2)

#include <iostream>

void main()
{
int a=16;
std::cout.setf(std::ios::hex, std::ios::basefield); //basefield specifies the base flag to be set
std::cout.setf(std::ios::showbase); //showbase is a generic single bit flag, needing no second parameter for clarification
std::cout << a << std::endl;
}




To output filler 0s, use the following functions...

#include <iostream>

void main()
{
int a=16;
std::cout.width(8);
std::cout.fill('0');
std::cout.setf(std::ios::internal, std::ios::adjustfield);
//internal tells cout to print filler characters between the 0x and the number
//adjustfield tells the function to modify the flags dealing with the field width format
std::cout << std::hex << std::showbase << a << std::endl;
}





Oh, and welcome back, for however long it lasts...

Edit: Fixed a couple things.

Share this post


Link to post
Share on other sites

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this