Hello.
This is code for testing a function that will be included in a larger program.
I'm trying to take an int from the user, then convert it to the equivalent binary number.
My approach so far is to create a string variable that will hold the binary value. I want my if statements to essentially append my string by either adding a character '1' or '0' onto it.
I've looked for examples online, but I don't know how to interpret them. Below you will likely find a confused interpretation.
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
usingnamespace std;
string dec_to_bin(int num){
string bin;
while(num > 0){
if( num % 2 == 1){
cout << "Your number is odd" << endl;
bin& += '1'; // Adds '1' to the resultant binary string
num = (num -1)/2;
}
else{
cout << " Number is even" << endl;
bin& += '0'; // Adds '0' to the resultant binary string
num = num/2;
}
}
}
int main(){
int num;
string ans;
cout << "enter int: " ;
cin >> num;
// validate int
ans = dec_to_bin(num);
return 0;
}
Also, I'm trying to come up with an idea for converting a binary number from the user to an integer using similarly simple operations seen here. If you have any ideas to share, that would also be very appreciated.
The easy way is to convert the integer into a hex string, and use a lookup table (16 values). Hex is directly convertible to binary. Same for the reverse. This gives you a printable version (string version) very easily (assuming this is what you want). Basically you need 0 to F in binary in a table of strings, then peel of the hex digit and spit out the binary string. so 0F spits out 00001111 and c++ has several ways you can get the hex string from an int value.
integers are already in binary format. you can use bitwise logic operations to print the bits in a loop.
Using brute force, this looks like:
1 2 3 4 5 6 7 8 9 10 11 12
int x = 1234;
for(int b = 2048; b > 0; b/=2)
{
if(x-b >= 0)
{
cout << 1;
x -= b;
}
else cout << 0 ;
}
there are slicker ways to get it, of course.
reverse whatever you do to take binary and get the integer.
This is probably a little more efficient because it creates only one string object. The trick is to build it up backwards and then reverse it at the end.
#include <iostream>
#include <string>
#include <algorithm>
using std::string;
using std::reverse;
string toBinary(int num)
{
string result;
int bit;
// Built up the result in reverse order
while (num) {
bit = num % 2;
result += '0' + bit;
num /= 2;
}
reverse(result.begin(), result.end()); // reverse the string
return result;
}
int
main()
{
int num;
while (std::cin >> num) {
std::cout << num << "\t-> " << toBinary(num) << '\n';
}
}
#include <iostream>
#include <string>
usingnamespace std;
string toBinary( unsigned n )
{
unsigned p = 2, ndigits = 1;
for ( ; p <= n; p <<= 1 ) ndigits++; // Increase power until 2^p > n
string result( ndigits, '0' ); // String of correct length
for ( unsigned i = 0; i < ndigits; i++ ) // Check all lower powers of 2
{
p >>= 1;
if ( n >= p )
{
result[i] = '1';
n -= p;
}
}
return result;
}
int main()
{
for ( unsigned n = 0; n < 32; n++ ) cout << n << '\t' << toBinary( n ) << '\n';
}