| | |
conversion to binary
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
Hey!
I just wanted to know that if there is any builtin function in c++ that converts a decimal number into binary number??
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
>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:
If you want an actual arithmetic value rather than a string, it's only marginally more difficult:
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)
#include <iostream> #include <bitset> #include <climits> int main() { int dec; std::cout<<"Enter a whole number: "; if ( std::cin>> dec ) std::cout<< std::bitset<sizeof dec * CHAR_BIT> ( dec ) <<'\n'; }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <bitset> #include <sstream> #include <climits> int main() { int dec; std::cout<<"Enter a whole number: "; if ( std::cin>> dec ) { std::istringstream iss ( std::bitset<sizeof dec * CHAR_BIT> ( dec ).to_string() ); unsigned long bit_num; iss>> bit_num; std::cout<< bit_num <<'\n'; } }
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
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..!
I would like to know what does this actually mean or how does this 'if statement' work?
Thanks
•
•
•
•
if ( std::cin>> dec )
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
>how does this 'if statement' work?
It's just a combination of these two statements:
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:
It's just a combination of these two statements:
C++ Syntax (Toggle Plain Text)
std::cin>> dec; if ( cin )
C++ has all kinds of shortcuts. One of them is being able to combine those two statements into one:
C++ Syntax (Toggle Plain Text)
if ( std::cin>> dec )
I'm here to prove you wrong.
![]() |
Similar Threads
- conversion to binary image (C++)
- Binary Fraction conversion question (C)
- Number System:Conversion from binary to decimal (C)
- Howdo I download a .mim file in Macintosh (OS X)
Other Threads in the C++ Forum
- Previous Thread: While loop not ending when reading from file
- Next Thread: beginning c++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






