RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 25045 | Replies: 3 | Thread Tools  Display Modes
Reply
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

decimal > binary > Oct > Hex

  #1  
Sep 12th, 2004
Would I have to go about doing the math manually and make my own function? Is there some built in function I can use to be lazy? Is there some totally easier way that I am oblivious to?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 12
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: decimal > binary > Oct > Hex

  #2  
Sep 12th, 2004
I found this link, which you could use to do the decimal to hex number.

But, I'd imagine somewhere there's bound to be some kind of library function that could do this for you... Have you searched Google for it yet?
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Apr 2004
Posts: 3,809
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: decimal > binary > Oct > Hex

  #3  
Sep 12th, 2004
Originally Posted by Transworld
Would I have to go about doing the math manually and make my own function? Is there some built in function I can use to be lazy? Is there some totally easier way that I am oblivious to?
I will assume you mean you would like to display the value of some integral type in decimal, binary, octal, and hexadecimal representations. Three out of four are available the lazy way, but you'll need to roll your own to display a binary representation.
#include <iostream> 
 
 template < typename T >
 inline T highbit(T& t)
 {
    return t = (((T)(-1)) >> 1) + 1;
 }
 
 template < typename T >
 std::ostream& bin(T& value, std::ostream &o)
 {
    for ( T bit = highbit(bit); bit; bit >>= 1 )
    {
 	  o << ( ( value & bit ) ? '1' : '0' );
    }
    return o;
 }
 
 
 int main() 
 {
    unsigned long value = 0x12345678;
    std::cout << "hex: " << std::hex << value << std::endl;
    std::cout << "dec: " << std::dec << value << std::endl;
    std::cout << "oct: " << std::oct << value << std::endl;
    std::cout << "bin: ";
    bin(value, std::cout);
    std::cout << std::endl;
    return 0; 
 }
 
 /* my output
 hex: 12345678
 dec: 305419896
 oct: 2215053170
 bin: 00010010001101000101011001111000
 */
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Re: decimal > binary > Oct > Hex

  #4  
Sep 12th, 2004
Yeah, I did search Google. Amazingly hard to find things sometimes... But thanks everyone.:cheesy:
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 7:57 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC