decimal > binary > Oct > Hex

Reply

Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

decimal > binary > Oct > Hex

 
0
  #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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: decimal > binary > Oct > Hex

 
0
  #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 Quick reply to this message  
Join Date: Apr 2004
Posts: 4,309
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: decimal > binary > Oct > Hex

 
0
  #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.
  1. #include <iostream>
  2.  
  3. template < typename T >
  4. inline T highbit(T& t)
  5. {
  6. return t = (((T)(-1)) >> 1) + 1;
  7. }
  8.  
  9. template < typename T >
  10. std::ostream& bin(T& value, std::ostream &o)
  11. {
  12. for ( T bit = highbit(bit); bit; bit >>= 1 )
  13. {
  14. o << ( ( value & bit ) ? '1' : '0' );
  15. }
  16. return o;
  17. }
  18.  
  19.  
  20. int main()
  21. {
  22. unsigned long value = 0x12345678;
  23. std::cout << "hex: " << std::hex << value << std::endl;
  24. std::cout << "dec: " << std::dec << value << std::endl;
  25. std::cout << "oct: " << std::oct << value << std::endl;
  26. std::cout << "bin: ";
  27. bin(value, std::cout);
  28. std::cout << std::endl;
  29. return 0;
  30. }
  31.  
  32. /* my output
  33.  hex: 12345678
  34.  dec: 305419896
  35.  oct: 2215053170
  36.  bin: 00010010001101000101011001111000
  37.  */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 14
Reputation: Transworld is an unknown quantity at this point 
Solved Threads: 0
Transworld Transworld is offline Offline
Newbie Poster

Re: decimal > binary > Oct > Hex

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

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC