944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3377
  • C++ RSS
Jan 20th, 2006
0

conversion to binary

Expand Post »
Hey!
I just wanted to know that if there is any builtin function in c++ that converts a decimal number into binary number??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
aminura is offline Offline
47 posts
since Oct 2004
Jan 20th, 2006
0

Re: conversion to binary

no, but Google is a good place to find out how to do it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jan 20th, 2006
0

Re: conversion to binary

>any builtin function in c++ that converts a decimal number into binary number??
Maybe, depending on what you want to do with it. If all you want is a string, then the bitset<> class is ideal:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <bitset>
  3. #include <climits>
  4.  
  5. int main()
  6. {
  7. int dec;
  8.  
  9. std::cout<<"Enter a whole number: ";
  10.  
  11. if ( std::cin>> dec )
  12. std::cout<< std::bitset<sizeof dec * CHAR_BIT> ( dec ) <<'\n';
  13. }
If you want an actual arithmetic value rather than a string, it's only marginally more difficult:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <bitset>
  3. #include <sstream>
  4. #include <climits>
  5.  
  6. int main()
  7. {
  8. int dec;
  9.  
  10. std::cout<<"Enter a whole number: ";
  11.  
  12. if ( std::cin>> dec ) {
  13. std::istringstream iss ( std::bitset<sizeof dec * CHAR_BIT> ( dec ).to_string() );
  14. unsigned long bit_num;
  15.  
  16. iss>> bit_num;
  17. std::cout<< bit_num <<'\n';
  18. }
  19. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 20th, 2006
0

Re: conversion to binary

Ohhh! pretty neat
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jan 21st, 2006
0

Re: conversion to binary

Thanks for the reply! I guess I would have to go through a tutorial of bitset classes to get an understanding of what it is and how it works..!

Quote ...
if ( std::cin>> dec )
I would like to know what does this actually mean or how does this 'if statement' work?

Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
aminura is offline Offline
47 posts
since Oct 2004
Jan 21st, 2006
0

Re: conversion to binary

>how does this 'if statement' work?
It's just a combination of these two statements:
C++ Syntax (Toggle Plain Text)
  1. std::cin>> dec;
  2. if ( cin )
Put simply, if ( cin ) says "Is the stream happy?". Since dec is an integer, if you typed a letter instead of a number, std::cin>>dec would fail with a conversion error, placing the stream in a failure state. Because the stream is in a failure state, if ( cin ) isn't true, so the body of the statement isn't executed. But if you type a valid integer, std::cin>>dec succeeds and doesn't set any failure flags, so if ( cin ) is true and the body of the statement is executed.

C++ has all kinds of shortcuts. One of them is being able to combine those two statements into one:
C++ Syntax (Toggle Plain Text)
  1. if ( std::cin>> dec )
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 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: While loop not ending when reading from file
Next Thread in C++ Forum Timeline: beginning c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC