Alright, thanks for the code examples above.
I will be studying them and will use them once I know and test them better.
Below is the hardway in that it demonstrates the math and could be extended to other bases.
As always your comments are welcome.
Dave++
Code:
#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <cstdlib>
std::string DecToBin(float num)
{
std::string str = "";
int i, expmax;
double top;
const double lg2 = log(2.0);
if(num == 0) return("0");
expmax = floor(log(num)/lg2);
// std::cout <<"expmax " << expmax << std::endl;
for(i=expmax; i>=0; i--){
top = num - pow(2.0,i); // subtract the binary basis
// std::cout << num << " " << top << std::endl;
if (top < 0){
str = str + "0";
}
else {
str = str + "1";
num = top;
}
}
return(str);
}
int main()
{
int num;
printf("Enter a whole number: ");
scanf("%i", &num);
std::cout << DecToBin(num) << std::endl;
// std::cout << log(2.0) << std::endl;
}
____________
If there are no spoons, then where did all the ice cream come from? ("7")