944,188 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 49216
  • C++ RSS
Sep 12th, 2004
0

decimal > binary > Oct > Hex

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004
Sep 12th, 2004
0

Re: decimal > binary > Oct > Hex

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?
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Sep 12th, 2004
0

Re: decimal > binary > Oct > Hex

Quote 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.
C++ Syntax (Toggle Plain Text)
  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.  */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 12th, 2004
0

Re: decimal > binary > Oct > Hex

Yeah, I did search Google. Amazingly hard to find things sometimes... But thanks everyone.:cheesy:
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Transworld is offline Offline
14 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can't download page from HTTPS
Next Thread in C++ Forum Timeline: A prayer to the C++ God pleaze help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC