conversion to binary

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

conversion to binary

 
0
  #1
Jan 20th, 2006
Hey!
I just wanted to know that if there is any builtin function in c++ that converts a decimal number into binary number??
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: conversion to binary

 
0
  #2
Jan 20th, 2006
no, but Google is a good place to find out how to do it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: conversion to binary

 
0
  #3
Jan 20th, 2006
>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:
  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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: conversion to binary

 
0
  #4
Jan 20th, 2006
Ohhh! pretty neat
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: conversion to binary

 
0
  #5
Jan 21st, 2006
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..!

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

Thanks
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: conversion to binary

 
0
  #6
Jan 21st, 2006
>how does this 'if statement' work?
It's just a combination of these two statements:
  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:
  1. if ( std::cin>> dec )
I'm here to prove you wrong.
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