char *toBin(unsigned long long num)
{
char s[9999];
const int size= 9999;
char *temp = new char[size];
int i, j;
int binBase = 2;
for(i=0; num!= 0; i++)
{
s[i] = num % binBase + '0';
num /= binBase;
}//get backwards binary
for(j=0; i>=0; j++)
{
i--;
temp[j]=s[i];
}//reverse binary sequence
//increments one more so need to decrement
temp[--j] = '\0';
return temp;
delete temp;
}
If I had "82" in a char array, I first converted the "82" to the decimal 82 and gave 82 to the function above to convert to binary. This only works within a certain limit of digits.
I think my problem is that unsigned long long can only hold a certain amount of digits so when it exceeds that limit, some random stuff sent to the function, which prints out the wrong binary.
The above function converts a unsigned long long num to binary. I realized that a better approach would be to look at each character in the char array, rather than converting it to a number first, and and use those values to convert to binary. However, I'm having a hard time trying to put it to code. Any suggestions or help would be appreciated.