Home c++ Decimal to Binary Conversion C++

# Decimal to Binary Conversion C++

Author

Date

Category

Hello fellow experts!
I wrote a program that converts decimal numbers to binary numbers. But there is one BUT.
It only works correctly with certain numbers. For example, if you enter 12 or 36
With others it is very crooked.
For example:

• Answer 100 is displayed for numbers from 4 to 7
• Reply 0100 is output from 8 to 11
• Answer 1100 is displayed from 12 to 15
• Answer 00100 is displayed from 16 to 19
• Answer 10100 is displayed from 20 to 23
• Answer 01100 is displayed from 24 to 27
• Answer 11100 is displayed between 28 and 31
• Answer 000100 is displayed from 32 to 35
• Response 100100 is displayed from 36 to 39

The next answer is 010100 from 40, etc.

I can’t figure it out myself, because I was asked to work with telephones. On this I ask for your help.
Here’s the actual code:
(He’s very primitive)

``````# include & lt; iostream & gt;
using namespace std;
int main ()
{
int num = 0, t = 0;
cout & lt; & lt; "Enter count:" & lt; & lt; endl;
cout & lt; & lt; "== & gt;";
cin & gt; & gt; num;
while (num)
{
num = num / 2;
t = num% 2;
cout & lt; & lt; t;
}
cout & lt; & lt; num;
cout & lt; & lt; endl;
system ("pause");
return 0;
}
``````

You are doing wrong. You are trying to construct a number so that, when printed as decimal, you get a binary representation of the number.

This is not correct, it is not necessary to do this, it will only lead to problems. It is necessary to distinguish between the number itself (a variable of a numeric type) and its representation in a particular number system (a string).

Try this:

``````# include & lt; string & gt;
std :: string to_binary_string (unsigned int n)
{
std :: string buffer; // response characters in reverse order
// allocate memory in advance to the maximum
buffer.reserve (std :: numeric_limits & lt; unsigned int & gt; :: digits);
do
{
buffer + = char ('0' + n% 2); // add to the end
n = n / 2;
} while (n & gt; 0);
return std :: string (buffer.crbegin (), buffer.crend ​​()); // expand the result
}
``````

For the case when you need to handle negative numbers, a little modification is needed:

``````# include & lt; string & gt;
#include & lt; limits & gt;
std :: string to_binary_string (int n)
{
if (n == std :: numeric_limits & lt; int & gt; :: min ())
return "-1" + std :: string (std :: numeric_limits & lt; int & gt; :: digits, '0');
std :: string buffer;
buffer.reserve (std :: numeric_limits & lt; int & gt; :: digits + 1); // +1 for minus
bool negative = (n & lt; 0);
if (negative)
n = -n;
do
{
buffer + = char ('0' + n% 2);
n = n / 2;
} while (n & gt; 0);
if (negative)
buffer + = '-';
return std :: string (buffer.crbegin (), buffer.crend ​​());
}
``````

This solution naturally generalizes to an arbitrary type:

``````# include & lt; string & gt;
#include & lt; limits & gt;
#include & lt; type_traits & gt;
template & lt; typename T & gt;
std :: string to_binary_string (T n)
{
static_assert (std :: is_integral & lt; T & gt; :: value, "Integer required.");
if (std :: numeric_limits & lt; T & gt; :: is_signed & amp; & amp;
n == std :: numeric_limits & lt; T & gt; :: min ()) // (*)
{
// overflow in signed type - UB, avoid it
return "-1" + std :: string (std :: numeric_limits & lt; T & gt; :: digits, '0');
}
std :: string buffer;
buffer.reserve (std :: numeric_limits & lt; T & gt; :: digits + 1); // +1 for possible minus
bool negative = (n & lt; 0);
if (negative)
n = -n; // this can be done by checking (*)
do
{
buffer + = char ('0' + n% 2);
n = n / 2;
} while (n & gt; 0);
if (negative)
buffer + = '-';
return std :: string (buffer.crbegin (), buffer.crend ​​());
}
``````

(Code partially stolen from this answer .)

The value `std :: numeric_limits & lt; T & gt; :: digits `represents the number of signed integer bits used to represent the value (excluding the sign bit and padding bits (padding bits )), respectively gives the number of binary digits in the longest string (excluding the negative minimum).

1. You cut off the last value when `num = num / 2; `

This should be at the end of the loop.

2. Values ​​must be printed in reverse order.

Here’s the changed code. Check it out.

here `d `is a digit. Because you need to print everything back.

``````# include & lt; iostream & gt;
using namespace std;
int main ()
{
int num = 0, t = 0, d = 1;
cout & lt; & lt; "Enter count:" & lt; & lt; endl;
cout & lt; & lt; "== & gt;";
cin & gt; & gt; num;
while (num)
{
t + = (num% 2) * d;
// cout & lt; & lt; t;
num = num / 2;
d = d * 10; // razryad
}
cout & lt; & lt; t;
// cout & lt; & lt; num;
cout & lt; & lt; endl;
system ("pause");
return 0;
}
``````

Here are 2 functions that I wrote a long time ago.

The first function converts integers from the 10th number system to any other number system from 2 to 36.

``````std :: string TransferDecimalToAny (long long int number, const unsigned int & amp; base)
{
const char * DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz";
bool FlagInverse = false;
if (base & lt; 2 || base & gt; 36)
{
std :: cout & lt; & lt; "Error: radix must be between 2 and 36." & lt; & lt; std :: endl;
return "-1";
}
if (number & lt; 0)
{
FlagInverse = true;
number * = -1;
}
stringInBase + = DIGITS [number% base];
number = number / base;
while (number)
{
stringInBase + = DIGITS [number% base];
number = number / base;
}
if (FlagInverse == true)
for (int i = stringInBase.length () - 1; i & gt; = 0; i--)
}
``````

And the second function also converts from the 10th number system to any other number system from 2 to 36, but only fractional numbers and with the specified accuracy accuracy .

``````std :: string TransferDecimalToAny (long double number, const unsigned int & amp; base, const int & amp; accuracy)
{
const char * DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz";
int iter = 0, tmp = 0;
bool FlagInverse = false;
if (number & lt; 0)
{
FlagInverse = true;
number * = -1;
}
long long int integer;
long double integer_tmp, fractional;
if (base & lt; 2 || base & gt; 36)
{
std :: cout & lt; & lt; "Error: radix must be between 2 and 36." & lt; & lt; std :: endl;
return "-1";
}
fractional = modf (number, & amp; integer_tmp);
integer = static_cast & lt; long long int & gt; (integer_tmp);
stringInBase + = DIGITS [integer% base];
integer = integer / base;
while (integer)
{
stringInBase + = DIGITS [integer% base];
integer = integer / base;
}
if (FlagInverse == true)
for (int i = stringInBase.length () - 1; i & gt; = 0; i--)
while (iter & lt; accuracy)
{
fractional * = base;
fractional = modf (fractional, & amp; integer_tmp);
tmp = static_cast & lt; int & gt; (integer_tmp);
iter ++;
}
}
``````

And call them accordingly as follows:

``````long long int number1 = -54605429;
long double number2 = -6546747.63768658;
std :: cout & lt; & lt; "Enter the translation base:";
std :: cin & gt; & gt; base;
std :: cout & lt; & lt; "Integer translation:" & lt; & lt; TransferDecimalToAny (number1, base) .c_str () & lt; & lt; std :: endl;
std :: cout & lt; & lt; "Translation of fractional:" & lt; & lt; TransferDecimalToAny (number2, base, 13) .c_str () & lt; & lt; std :: endl;
``````

Here is the option without divisions.

``````# include & lt; iostream & gt;
char * dec_to_bin (char * s, int n) {
unsigned int u = (unsigned int) n;
char * t = s;
do {
* s ++ = (char) ((u & amp; 1) + '0');
} while ((u & gt; & gt; = 1)! = 0);
* s-- = '\ 0';
char c;
for (char * p = t; p & lt; s; ++ p, --s) {
c = * p;
* p = * s;
* s = c;
}
return t;
}
int main (void) {
char buf [(sizeof (int) & lt; & lt; 3) + 1];
std :: cout & lt; & lt; dec_to_bin (buf, 2012) & lt; & lt; std :: endl;
std :: cout & lt; & lt; dec_to_bin (buf, -2012) & lt; & lt; std :: endl;
return 0;
}
``````

``````# include & lt; iostream & gt;
using namespace std;
int main ()
{
int x, a;
cin & gt; & gt; a;
for (int i = 31; i & gt; = 0; i--)
{
x = ((a & gt; & gt; i) & amp; 1);
cout & lt; & lt; x & lt; & lt; '';
}
cout & lt; & lt; endl;
return 0;
}
``````

``````int a;
int tmp = 1;
std :: cin & gt; & gt; a;
if (a & lt; 0) {
a * = -1;
std :: cout & lt; & lt; '-';
}
if (a == 0) std :: cout & lt; & lt; 0;
else {
while (tmp & lt; = a) tmp = tmp & lt; & lt; 1;
while (tmp & gt; 1) {
tmp = tmp & gt; & gt; 1;
std :: cout & lt; & lt; (((tmp & amp; a)! = 0)? 1: 0);
}
}
``````

``````Here is the C++ code for decimal to binary conversion. I wrote it myself, because I have not found an answer on the Internet. The code is as simple as possible. I wrote it as a separate function.
#include "stdafx.h"
#include & lt; iostream & gt;
#include & lt; cmath & gt;
using namespace std;
void function (int a) {
long long int n = 0;
int k, m = 0;
while (a) {
k = a% 2;
a = a / 2;
n + = k * pow (10, m);
m ++;
}
cout & lt; & lt; n & lt; & lt; endl;
}
int main ()
{
int a;
cin & gt; & gt; a;
function (a);
return 0;
}
``````

implementation using bitwise shift.

``````# include & lt; iostream & gt;
#include & lt; cmath & gt;
using namespace std;
void to_binar (int a, string & amp; s)
{
int mx = 0;
while ((a & gt; & gt; mx ++) & gt; 1);
int num = a, bin;
while (mx & gt; 0)
{
bin = pow (2, --mx);
if (num & lt; bin)
s + = '0';
else
{
s + = '1';
num - = bin;
}
}
}
int main ()
{
int a1 = 77, a2 = 76;
string s1, s2;
to_binar (a1, s1);
to_binar (a2, s2);
cout & lt; & lt; s1 & lt; & lt; endl & lt; & lt; s2 & lt; & lt; endl;
return 0;
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.