c++ decimal to binary program help

hi i need help i want convert decimal to binary c++ program i made it. But I do not want to use an integer i want convert example 5.2 , 61,3 , 22,4 , 33,50 , 1,7 , 99,33 ... any one help me ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
 int num,arr[64];
 cout<<"Enter a number : ";
 cin>>num;
 int i=0,r;
 while(num!=0)
{
  r = num%2;
  arr[i++] = r;
  num /= 2;
}
 cout<<"Binary :";
for(int j=i-1;j>=0;j--)
  cout<<arr[j];
}
Last edited on
Hello harock,

If I understand you correctly you want to convert an octal number of 5.2 to a binary number. I will not say that it is impossible or can not be done, but I have not heard of this before.

You might have to break up a floating point number into a left and right set of numbers, convert, then put back together. Just a guess for now.

You might find something useful here:

https://www.google.com/#safe=strict&q=c%2B%2B+octal+to+floating+point+binary

https://codereview.stackexchange.com/questions/25824/floating-point-binary-decimal-octal-hex-converter

The second I have not read through all of it yet, so I do not know if it applies.

Hope that helps,

Andy
Thank you for reply it s my mistake i want convert decimal to binary but dont integer
Hello harock,

On one hand I have a better understanding of what you want decimal to binary. Your program does that. On the other hand I do not see any advantage of changing "num" to something other than "int" other than a "short" would limit the range of what "num" can use.

Many people have done this before. You should find something useful with a search here on "decimal to binary".

No offense, but I think that English is not your first language and that is causing a problem with me understanding what you want.

Andy
Hello Andy
You re right English not my first language and program convert decimal to binary there s no problem but i want convert decimal (not integer ) like 3,4 66,7 12,2 ... to binary i googleing about decimal to bnary but every example work on integer i dnt want integer i want convert Comma-separated number i hope wii understand
i hope you understand me :) Thank you everything

Uses a period '.' for the decimal point, rather than a comma ','. Feel free to change that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath>
using namespace std;

char point = '.';

string decToBase( double x, int base, int fracDigits )
{
   string result;
   bool neg = ( x < 0 );   if ( neg ) x = -x;

   // Allow for potential rounding to given precision
   x += pow( base, -fracDigits ) / 2.0;

   // Split into whole number and fractional parts
   int whole = x;
   double fraction = x - whole;
   
   // Whole-number part
   if ( whole == 0 ) result = "0";
   while ( whole > 0 )
   {
      result = (char)( whole % base + '0' ) + result;
      whole /= base;
   }

   result += point;

   // Fractional part
   double value = 1.0;
   for ( int i = 1; i <= fracDigits; i++ )
   {
      value /= base;
      int num = fraction / value;
      result += (char)( num + '0' );
      fraction -= num * value;
   }

   // Deal with sign
   if ( neg ) result = '-' + result;

   return result;
}

int main()
{
   int prec = 8;    // digits after point
   int base = 2;    // number base
   double x;

   cout << "Input double: ";
   cin >> x;

   cout << x << " in base " << base << " is " << decToBase( x, base, prec );
}
Last edited on
Hello harock,

The language and country differences has caught me off guard. I am not that familiar with people using "3,4" for what I am use to as "3.4", but I know it is used. "3,4" and "3.4" can be refereed to as a decimal number, but better to call it a floating point number if there is any doubt especially if you use a comma.

If you want to use a floating point double then lastchance has a good solution.

I do not remember what I used for a search, either "c++ decimal to binary", "c++ double to binary" or maybe "c++ floating point to binary". Something there should produce some results that you could use.

Hope that helps,

Andy
Topic archived. No new replies allowed.