A simple C++ program to convert floating point numbers to binary. Works by using modf to separate the fractional and integer part of the number. Performs integer to binary calculation on the integer part and then it performs fractional to binary calculation on the fractional part. Source Link.
Decimal to Binary
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
// limit to how long the binary string is.
#define MAX_BIN 1077
/* Mao ni ang function para sa integer na part */
void f2binI(double fInt, char* strBinary) {
int iCount = 0;
int i;
char strBuffer[MAX_BIN];
do {
strBuffer[iCount++] = '0' + (int)fmod(fInt,2);
fInt = floor(fInt/2);
} while (fInt > 0);
/* Reverse the binary string */
for (i=0; i<iCount; i++)
strBinary[i] = strBuffer[iCount-i-1];
strBinary[iCount] = 0; //Null terminator
}
/* Mao ni ang function para sa fractional na part */
void f2binF(double fFrac, char* strBinary) {
int iCount = 0;
double fInt;
while (fFrac > 0)
{
fFrac*=2;
fFrac = modf(fFrac,&fInt);
strBinary[iCount++] = '0' + (int)fInt;
}
strBinary[iCount] = 0; //Null terminator
}
/* automatic na ni xa mo decide gamit modf para
i convert ang integer ug fractional na part. */
void f2bin(double fp, char* strBinary) {
double fInt, fFrac;
/* Separate integer and fractional parts */
fFrac = modf(fp,&fInt);
/* Convert integer part, if any */
if (fInt != 0)
f2binI(fInt,strBinary);
else
strcpy(strBinary,"0");
strcat(strBinary,"."); // Radix point
/* Convert fractional part, if any */
if (fFrac != 0)
f2binF(fFrac,strBinary+strlen(strBinary)); //Append
else
strcpy(strBinary+strlen(strBinary),"0");
}
int main()
{
double fInput;
cout << "Enter a floating point number: ";
cin >> fInput;
char strBinary[MAX_BIN];
f2bin(fInput,strBinary);
cout << "Float " << fInput << " = " << strBinary;
return 0;
}
Source: http://j.mp/eBmv83
Decimal to Binary in C
// Convert a decimal integer do a binary string
// added a test printf() you can remove later
// Turbo C modified for Pelles C vegaseat 19nov2004
#include <stdio.h>
void dec2bin(long decimal, char *binary);
int main()
{
long decimal;
char binary[80];
printf("\n\n Enter an integer value : ");
scanf("%ld",&decimal);
dec2bin(decimal,binary);
printf("\n The binary value of %ld is %s \n",decimal,binary);
getchar(); // trap enter
getchar(); // wait
return 0;
}
//
// accepts a decimal integer and returns a binary coded string
//
void dec2bin(long decimal, char *binary)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
int old_decimal; // for test
char temp[80];
// take care of negative input
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
old_decimal = decimal; // for test
remain = decimal % 2;
// whittle down the decimal number
decimal = decimal / 2;
// this is a test to show the action
printf("%d/2 = %d remainder = %d\n", old_decimal, decimal, remain);
// converts digit 0 or 1 to character '0' or '1'
temp[k++] = remain + '0';
} while (decimal > 0);
if (neg_flag)
temp[k++] = '-'; // add - sign
else
temp[k++] = ' '; // space
// reverse the spelling
while (k >= 0)
binary[n++] = temp[--k];
binary[n-1] = 0; // end with NULL
}
This version does not have functions.
Source: http://j.mp/idz1A5
Decimal to Binary in C++
#include<iostream>
using namespace std;
int bin(int num);
int main()
{
cout<<"Enter a <strong class="highlight">binary</strong> number ";
int num;
cin>>num;
cout<<endl<<endl;
bin(num);
cout<<endl<<endl;
system("PAUSE");
return 0;
}
int bin(int num)
{
int a[25];
int i;
i=0;
int r;
int d;
int b;
int j;
j= 0;
cout<<"<strong class="highlight">Binary</strong> is : ";
while(num>0)
{
r=num%2;
a[j]=r;
d=num/2;
b=num;
num=d;
j++;
}
while(j>=0)
{
cout<<a[j];
j--;
}
}
This version uses malloc to resize array pointers for functions.
Source: http://j.mp/ieA825
Decimal to Binary in C++ Using Functions and Pointers
#include<iostream>
#include<stdlib.h>
using namespace std;
class d2b
{
public:
int dec2bin(int *p)
{
int a,b,c=1,d=0;
while (*p!=0)
{
a = *p%2;
b = a * c;
d = d + b;
*p = *p/2;
c = c * 10;
}
return d;
}
};
int main()
{
d2b db;
int *n,x,y;
n = (int*)malloc(sizeof(int));
cout<<"Enter the decimal number: ";
cin>>*n;
y = *n;
x = db.dec2bin(n);
cout<<"The binary conversion of "<<y<<" is "<<x<<endl;
return 0;
}
This version can convert floats and doubles by dividing the number to its fractional and integer parts. Another algorithm is used to convert the fractional part in to binary.
Source: http://j.mp/fcIrnE
Float to Binary in C++
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
// limit to how long the binary string is.
#define MAX_BIN 1077
/* Mao ni ang function para sa integer na part */
void f2binI(double fInt, char* strBinary) {
int iCount = 0;
int i;
char strBuffer[MAX_BIN];
do {
strBuffer[iCount++] = '0' + (int)fmod(fInt,2);
fInt = floor(fInt/2);
} while (fInt > 0);
/* Reverse the binary string */
for (i=0; i<iCount; i++)
strBinary[i] = strBuffer[iCount-i-1];
strBinary[iCount] = 0; //Null terminator
}
/* Mao ni ang function para sa fractional na part */
void f2binF(double fFrac, char* strBinary) {
int iCount = 0;
double fInt;
while (fFrac > 0)
{
fFrac*=2;
fFrac = modf(fFrac,&fInt);
strBinary[iCount++] = '0' + (int)fInt;
}
strBinary[iCount] = 0; //Null terminator
}
/* automatic na ni xa mo decide gamit modf para
i convert ang integer ug fractional na part. */
void f2bin(double fp, char* strBinary) {
double fInt, fFrac;
/* Separate integer and fractional parts */
fFrac = modf(fp,&fInt);
/* Convert integer part, if any */
if (fInt != 0)
f2binI(fInt,strBinary);
else
strcpy(strBinary,"0");
strcat(strBinary,"."); // Radix point
/* Convert fractional part, if any */
if (fFrac != 0)
f2binF(fFrac,strBinary+strlen(strBinary)); //Append
else
strcpy(strBinary+strlen(strBinary),"0");
}
int main()
{
double fInput;
cout << "Enter a floating point number: ";
cin >> fInput;
char strBinary[MAX_BIN];
f2bin(fInput,strBinary);
cout << "Float " << fInput << " = " << strBinary;
return 0;
}